Reputation: 581
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?
Upvotes: 2
Views: 1334
Reputation: 1624
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'
Upvotes: 1