pandas .drop
时间: 2024-01-11 20:22:43 浏览: 77
pandas的drop方法用于删除DataFrame或Series中的行或列。下面是两个例子:
1. 删除DataFrame中的行或列:
```python
import pandas as pd
import numpy as np
df1 = pd.DataFrame(np.arange(36).reshape(6,6), columns=list('ABCDEF'))
print("原始DataFrame:")
print(df1)
# 删除列
df2 = df1.drop(['B', 'D'], axis=1)
print("删除列后的DataFrame:")
print(df2)
# 删除行
df3 = df1.drop([2, 4])
print("删除行后的DataFrame:")
print(df3)
```
2. 删除Series中的元素:
```python
import pandas as pd
import numpy as np
s = pd.Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
print("原始Series:")
print(s)
# 删除指定的index label
s2 = s.drop('c')
print("删除指定index label后的Series:")
print(s2)
# 删除多个index label
s3 = s.drop(['d', 'c'])
print("删除多个index label后的Series:")
print(s3)
```
阅读全文