Reputation: 1345
All,
I would like to update an existing column in pandas. Here is an example:
df = pd.DataFrame({'a': ['A', 'B', 'C', 'A'], 'b': ['C', 'A', 'B', 'C'], 'c': np.random.randn(4), 'd': np.random.randn(4) })
df['NewColumn1'] = np.where( df['a'].str.contains('A') & df['b'].str.contains('C') , df['c'], 1)
df
a b c d NewColumn1
0 A C -0.668001 -0.434521 -0.668001
1 B A 1.893248 1.927666 1.000000
2 C B -2.145310 0.602808 1.000000
3 A C 0.323770 0.966303 0.323770
I have create a new column called 'NewColumn1'.
Now I would like to update NewColumn1 based on new condition (my aim here is to update NewColumn1 not overwrite its previous values).
I would like to update 'NewColumn1' and set its values to 100 when column 'd' is bigger than 1. How can I do that?
Many Thanks
Upvotes: 1
Views: 1809
Reputation: 863801
You need:
df['NewColumn1'] = np.where( df['d'] > 1 , 100, df['NewColumn1'])
Upvotes: 1
Reputation: 164853
You can use .loc
accessor to update an existing series:
df.loc[df['d'] > 1, 'NewColumn1'] = 100
Your question is still unclear: "Update but not overwrite previous values" does not make sense.
In future, provide your desired output. This is much more valuable than a lengthy description.
Upvotes: 2