0

I am coming from R background. I need elementary with pandas.

if I have a dataframe like this

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6]]))

I want to subset dataframe to select a fixed column and select a row by a boolean.

For example

df.iloc[df.2 > 4][2]

then I want to set the value for the subset cell to equal a value. something like

df.iloc[df.2 > 4][2] = 7

It seems valid for me however it seem pandas work with booleans in more strict way than R

Rohitashwa Nigam
  • 399
  • 5
  • 16
Omar113
  • 210
  • 1
  • 7

1 Answers1

1

In here it is .loc

df.loc[df[2] > 4,2]
1    6
Name: 2, dtype: int64

df.loc[df[2] > 4,2]=7
df
   0  1  2
0  1  2  3
1  4  5  7
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thank you very much that was a quick solve. However I have another question if you may! Why I can't mix loc() with either names or index. In R I can refer to column either by name or index however here (in another dataframe), It requires me to make both rows and columns named?! – Omar113 Apr 14 '19 at 17:49
  • 1
    @Omar113 index you need .iloc for name you can using .loc , and if you want to mix , you can look at .at – BENY Apr 14 '19 at 17:54