2

I have a dataframe of people and rather than editing it in excel I would like to be able to enter data in pandas directly.

In the example table below, John has nothin in df['Group']

DF

Name ID Grade Group
0 John 001 89 Nan
1 Jane 002 56 Group 1
2 Joan 003 91 Group 2
3 David 004 45 Group 1

I'd like to be able to change this to 'Group 4', preferably without using iloc (it's a big-ish df).

Name ID Grade Group
0 John 001 89 Group 4
1 Jane 002 56 Group 1
2 Joan 003 91 Group 2
3 David 004 45 Group 1

I have tried

df.loc[df['Name'] == 'John']['Group'] = 'Group 4'

df2.loc[0,'Group'] = 'Group 4'

These don't work (probably obvious to you).

Is there a way to do this?

2
  • 1
    df.loc[df['Name'] == 'John','Group'] = 'Group 4'. Commented Mar 18, 2021 at 14:11
  • 1
    df.loc[df['Name'] == 'John','Group'] = 'Group 4' if you want to do it by Name of df.Group.fillna('Group 4',inplace=True) if you want to fill all 'NaN's (make sure it is NaN) in Group column to be 'Group4'
    – moys
    Commented Mar 18, 2021 at 14:15

1 Answer 1

1

For assigning new variables in pandas you can use either loc or at method by having name of the labels:

loc method:

df.loc[df['name'] == 'John', 'Group'] = 'Group 4'

at method(Caveat:at can only access a single value at a time)

df.at[df['name'] == 'John', 'Group'] = 'Group 4'
# **OR**
df.at[0, 'Group'] = 'Group 4'
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.