I have a DataFrame like this:
upper level1 level2
lower name name
1 Mary Tom
2 ... ...
What if I want to add another column to level1? for instance
upper level1 level2
lower name age name
1 Mary 13 Tom
2 ... ... ...
I can access the data using df['level1'].loc[:,'name'], but I do not know how to add / remove a column.
If I just use df.level1['age']=1Python returns a copy warning and nothing changes in the DataFrame:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: http:
if __name__ == '__main__':
source
share