0

Given a Pandas dataframe with A as an index:

A   B   C
1   2   5
2   5   8
3   6   3
4   8   2

Knowing index 3, what is the best way to get:

A   B   C
1   2   5
2   5   8
3   Nan 3
4   Nan 2

In the real example my index is a datetime and the values are floats, but that hopefully will not affect things.

SuperStormer
  • 3,554
  • 4
  • 16
  • 29
Felix
  • 1,302
  • 1
  • 11
  • 24
  • 1
    Try `df.loc[2:, "B"] = np.nan`? – Chris Dec 18 '20 at 01:01
  • That worked but gave this message "FutureWarning: Slicing a positional slice with .loc is not supported, and will raise TypeError in a future version. Use .loc with labels or .iloc with positions instead." – Felix Dec 18 '20 at 01:06
  • I reset the index and worked based on row and the warning went away. – Felix Dec 18 '20 at 01:08
  • 1
    I'm guessing `df` was a reference of another dataframe, instead of being a copy. Can you try `df.is_copy` to check? or more aggressively, do `df = df.copy()` to create one? – Chris Dec 18 '20 at 01:08
  • I don't understand why a question is closed just because there are other so called "similar" questions. Looking at those other questions just makes me more confused rather than less. One may as well just suggest reading the dataframe documentation. I think the "similar" question criteria are being applied a little aggressively here – Felix Dec 18 '20 at 01:32
  • This [answer](https://stackoverflow.com/a/65315058/7758804), from the duplicate, specifically answers your question, as do many of the other answers (to a greater or lesser) degree. – Trenton McKinney Dec 18 '20 at 04:08
  • Trenton, I think the question you linked was to set a single cell. .loc() is not mentioned until the third answer down. The comments by Chris actually got me there, and I would set as the answer if I could. – Felix Dec 20 '20 at 20:57

0 Answers0