I'm new to python and pandas, and I don't understand why sometimes I can copy or manipulate dataframe columns and sometimes I can't. Here is a dataframe called df:
A B C
0 hello hola bonjour
1 goodbye adios au revoir
My first modification will be overwriting column 'C' row by row with a for-loop (yes, there may be better ways to do this - that's not the point), and the result is as I expected:
for index,row in df.iterrows():
row['C'] = row['A']
A B C
0 hello hola hello
1 goodbye adios goodbye
I can add a new column using the following for-loop, and again, I get what I expected:
for index,row in df.iterrows():
df.ix[index,'D'] = len(row['C'])
A B C D
0 hello hola hello 5
1 goodbye adios goodbye 7
Now I try almost exactly the same thing as my first modification (overwriting column 'B' row by row with a for-loop), but this time it doesn't work. The dataframe does not change this time.
for index,row in df.iterrows():
row['B'] = row['A']
A B C D
0 hello hola hello 5
1 goodbye adios goodbye 7
I want to know 2 things:
1) Why does the same code overwrite a column some times, but not other times?
2) Am I doing something wrong that causes pandas to behave in an unintuitive manner like this? If so, what is the proper way to construct one column from another so that this kind of thing doesn't happen?
Any good answer or advice is much appreciated. Thanks!