4

I have a DataFrame like this:

upper    level1    level2 
lower    name      name
1        Mary      Tom
2        ...       ...

What should I do if I want to add another column under level1? For example

upper    level1       level2 
lower    name    age  name
1        Mary    13   Tom
2        ...    ...    ...

I can access data with df['level1'].loc[:,'name'], but I don't know how to add/remove a column.

If I just use df.level1['age']=1, Python returns a copy warning and nothing changed 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://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
if __name__ == '__main__':
piRSquared
  • 285,575
  • 57
  • 475
  • 624
Arthur
  • 159
  • 3
  • 11

2 Answers2

6

Try this:

df.insert(1, ('level1', 'age'), pd.Series([13]))
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • it's so easy and elegant! – MaxU - stand with Ukraine Jul 06 '16 at 19:18
  • Thanks it works! Could you please explain why my code is incorrect? – Arthur Jul 06 '16 at 19:22
  • @Arthur see http://stackoverflow.com/a/38147527/2336654 for a good example of what is happening with `SettingWithCopyWarning`. In your case, `df.level1` is returning a slice or view of `df` and you are trying to alter `df` via the slice. Pandas will make a copy of `df.level1` and perform the alteration on the copy instead. Often this isn't what you want and pandas is warning you. – piRSquared Jul 06 '16 at 19:27
  • @piRSquared Thank you. – Arthur Jul 06 '16 at 19:36
5

You can use tuple in the assignment:

In [11]: df[('level1', 'age')] = 13  # or a Series here, rather than a number

In [12]: df
Out[12]:
  upper level1 level2 level1
  lower   name   name    age
0     1   Mary    Tom     13
1     2    ...    ...     13
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • Thank you! But could you please tell me why my code is wrong? I'm new to Python. – Arthur Jul 06 '16 at 19:25
  • @Arthur when doing df.level1 it creates a view which is the subdataframe which has only the level1 columns. Trying to create a new column on the view (with `df.level1['age']=1`) doesn't work, as you can't edit the "view" (it's readonly). – Andy Hayden Jul 06 '16 at 20:14