简体   繁体   中英

Pandas DataFrame drop tuple or list of columns

When using the drop method for a pandas.DataFrame it accepts lists of column names, but not tuples, despite the documentation saying that " list-like " arguments are acceptable. Am I reading the documentation incorrectly, as I would expect my MWE to work.

MWE

import pandas as pd
df = pd.DataFrame({k: range(5) for k in list('abcd')})
df.drop(['a', 'c'], axis=1) # Works
df.drop(('a', 'c'), axis=1) # Errors

Versions - Using Python 2.7.12, Pandas 0.20.3.

Pandas treats tuples as multi-index values, so try this instead:

In [330]: df.drop(list(('a', 'c')), axis=1)
Out[330]:
   b  d
0  0  0
1  1  1
2  2  2
3  3  3
4  4  4

here is an example for deleting rows (axis=0 - default) in the multi-index DF:

In [342]: x = df.set_index(np.arange(len(df), 0, -1), append=True)

In [343]: x
Out[343]:
     a  b  c  d
0 5  0  0  0  0
1 4  1  1  1  1
2 3  2  2  2  2
3 2  3  3  3  3
4 1  4  4  4  4

In [344]: x.drop((0,5))
Out[344]:
     a  b  c  d
1 4  1  1  1  1
2 3  2  2  2  2
3 2  3  3  3  3
4 1  4  4  4  4

In [345]: x.drop([(0,5), (4,1)])
Out[345]:
     a  b  c  d
1 4  1  1  1  1
2 3  2  2  2  2
3 2  3  3  3  3

So when you specify tuple Pandas treats it as a multi-index label

There is problem with tuples select Multiindex :

np.random.seed(345)
mux = pd.MultiIndex.from_arrays([list('abcde'), list('cdefg')])

df = pd.DataFrame(np.random.randint(10, size=(4,5)), columns=mux)
print (df)
   a  b  c  d  e
   c  d  e  f  g
0  8  0  3  9  8
1  4  3  4  1  7
2  4  0  9  6  3
3  8  0  3  1  5

df = df.drop(('a', 'c'), axis=1)
print (df)
   b  c  d  e
   d  e  f  g
0  0  3  9  8
1  3  4  1  7
2  0  9  6  3
3  0  3  1  5

Same as:

df = df[('a', 'c')]
print (df)
0    8
1    4
2    4
3    8
Name: (a, c), dtype: int32

I used this to delete column of tuples

del df3[('val1', 'val2')]

and it got deleted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM