I have a very large dataframe with 108 columns and 8000 rows, and I want to drop some of the columns. It doesn't have a specific column name, it only has index on as column name.
the columns I want to remove is from 74 to 104.
I have tried:
df.drop(['74', '104'], axis = 1, inplace = True)
but it said:
['74' '104'] not found in axis
which 74 and 104 are the column index I want to remove.
How can I fix this problem?
Thank you in advance
When you refer to columns using index, you should treat them as integers.
df.drop([74, 104], axis=1, inplace=True)
Example:
>>> df = pd.DataFrame([[1,2,3],[4,5,6]])
>>> df
0 1 2
0 1 2 3
1 4 5 6
>>> df.drop([1], axis=1)
0 2
0 1 3
1 4 6
drop column by index, example:
data= {1:[1,19,20,21,25,29,30,31,30,29,31],
2: [2,10,20,20,20,10,10,20,20,10,10],
3: [3,10,20,20,20,10,10,20,20,10,10],
4: [4,10,20,20,20,10,10,20,20,10,10]}
index= pd.date_range('12/1/2019', periods=11)
df=pd.DataFrame(data, index=index)
df
will give you
1 2 3 4
2019-12-01 1 2 3 4
2019-12-02 19 10 10 10
2019-12-03 20 20 20 20
2019-12-04 21 20 20 20
2019-12-05 25 20 20 20
2019-12-06 29 10 10 10
2019-12-07 30 10 10 10
2019-12-08 31 20 20 20
2019-12-09 30 20 20 20
2019-12-10 29 10 10 10
2019-12-11 31 10 10 10
and then (say you wish to drop the INDEX of column 0,1,2):
df.drop(df.columns[[0,1,2]], axis=1, inplace=True)
desired result:
4
2019-12-01 4
2019-12-02 10
2019-12-03 20
2019-12-04 20
2019-12-05 20
2019-12-06 10
2019-12-07 10
2019-12-08 20
2019-12-09 20
2019-12-10 10
2019-12-11 10
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.