Assuming I've the following data frame:
import pandas as pd
df = pd.DataFrame(['a', 'b', 'c', 'd', 'a', 'c', 'f', 'a'])
print(df)
I can replace any occurrence of 'a' with 'AAA' as follows:
df.columns = ['Letters']
for i, x in enumerate(df['Letters']):
if x == 'a':
df['Letters'][i] = "AAA"
print(df)
But if I extracted unique row and try to do the same thing, it does not work.
df = pd.DataFrame(['a', 'b', 'c', 'd', 'a', 'c', 'f', 'a'])
df.columns = ['Letters']
grouped = df.groupby('Letters')
index = [gp_keys[0] for gp_keys in grouped.groups.values()]
unique_df = df.reindex(index)
print(unique_df)
for i, x in enumerate(unique_df):
if x == 'a':
unique_df.loc[i] = "AAA"
print(unique_df)
I am curious why doing unique_df[i] = "AAA" no longer modifies the data frame values. Even doing unique_df.loc[i] = "AAA" as suggested in the view versus copy post here seems to make no difference. It seems there is something about the groupby function that makes later modification on the data frame elusive. Any thoughts?
df['Letters']vsunique_dfin the iteration/assignment. So in the second case it tries to set theith column (it is 'Letters', not 'First'). If you replaceunique_dfwithunique_df['Letters'], it works. But anyway, you should just better dodf.loc[df['Letters']=='a', 'Letters'] = "AAA"instead of the for loop.for i, x in enumerate(unique_df): if x == 'a': unique_df.loc[i] = "AAA" print(unique_df)