2024-03-28 21:29:26 发布
网友
从数据帧中删除列的最常见方法似乎是del df["column_name"]
del df["column_name"]
然而,del df.loc[:,column_name]不起作用,即使df["column_name"]和df.loc[:,common_name]基本上做相同的事情
del df.loc[:,column_name]
df["column_name"]
df.loc[:,common_name]
我真的很好奇,因为我希望有一种同样简单的删除行的方法。无法使用直接df[]语法访问行,因此我必须使用del df.loc[],正如我所描述的,即使我处理的是列,这也不起作用
df[]
del df.loc[]
为什么{}不起作用,即使{}起作用
至于为什么del df.loc[:, column]不起作用,我假设因为它没有被烘焙到API中,所以开发人员制作.drop是有原因的
del df.loc[:, column]
.drop
You can read here more,即使是《熊猫》的主要作者Wes也推荐了《熊猫》的^{,但请参阅其他答案和评论,以便对最佳实践进行更广泛的讨论
从文件中
Drop specified labels from rows or columns.Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names. When using a multi-index, labels on different levels can be removed by specifying the level.
Drop specified labels from rows or columns.
Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index or column names. When using a multi-index, labels on different levels can be removed by specifying the level.
0
1
import pandas as pd df = pd.DataFrame({'num_legs': [2, 4, 8, 0], 'num_wings': [2, 0, 0, 0], 'num_specimen_seen': [10, 2, 1, 8]}, index=['falcon', 'dog', 'spider', 'fish']) print(df) num_legs num_wings num_specimen_seen falcon 2 2 10 dog 4 0 2 spider 8 0 1 fish 0 0 8 print(df.drop(['falcon','dog'],0)) num_legs num_wings num_specimen_seen spider 8 0 1 fish 0 0 8
s = pd.Series(list('abca')) df = pd.get_dummies(s) print(df) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 print(df.drop([0,2],0)) a b c 1 0 1 0 3 1 0 0
print(df.drop(['a'],1)) b c 0 0 0 1 1 0 2 0 1 3 0 0
至于为什么
del df.loc[:, column]
不起作用,我假设因为它没有被烘焙到API中,所以开发人员制作.drop
是有原因的You can read here more,即使是《熊猫》的主要作者Wes也推荐了《熊猫》的^{,但请参阅其他答案和评论,以便对最佳实践进行更广泛的讨论
您可以使用^{}
从文件中
放置在命名索引上,为索引指定轴
0
1
表示列基于基于整数的索引删除
删除一列
相关问题 更多 >
编程相关推荐