Python Pandas-在一个命令中从数据框中删除多个序列 [英] Python Pandas - Deleting multiple series from a data frame in one command
问题描述
简而言之...我有一个Python Pandas数据框,可使用'read_table'从Excel文件中读取该数据框.我想从数据中保留几个系列,然后清除其余的系列.我知道我可以使用'del data ['SeriesName']'逐个删除不想要的内容,但是我宁愿做的是指定要保留的内容,而不是指定要删除的内容.>
如果最简单的答案是将现有数据帧复制到仅包含我想要的序列的新数据帧中,然后将其全部删除,我会对该解决方案感到满意...但是如果那是确实是最好的方法,有人可以引导我完成它吗?
TIA ...我是Pandas的新手. :)
您可以使用DataFrame
drop
函数删除列.您必须通过axis=1
选项,它才能在列而不是行上工作.请注意,它返回一个副本,因此您必须将结果分配给新的DataFrame
:
In [1]: from pandas import *
In [2]: df = DataFrame(dict(x=[0,0,1,0,1], y=[1,0,1,1,0], z=[0,0,1,0,1]))
In [3]: df
Out[3]:
x y z
0 0 1 0
1 0 0 0
2 1 1 1
3 0 1 0
4 1 0 1
In [4]: df = df.drop(['x','y'], axis=1)
In [5]: df
Out[5]:
z
0 0
1 0
2 1
3 0
4 1
In short ... I have a Python Pandas data frame that is read in from an Excel file using 'read_table'. I would like to keep a handful of the series from the data, and purge the rest. I know that I can just delete what I don't want one-by-one using 'del data['SeriesName']', but what I'd rather do is specify what to keep instead of specifying what to delete.
If the simplest answer is to copy the existing data frame into a new data frame that only contains the series I want, and then delete the existing frame in its entirety, I would satisfied with that solution ... but if that is indeed the best way, can someone walk me through it?
TIA ... I'm a newb to Pandas. :)
You can use the DataFrame
drop
function to remove columns. You have to pass the axis=1
option for it to work on columns and not rows. Note that it returns a copy so you have to assign the result to a new DataFrame
:
In [1]: from pandas import *
In [2]: df = DataFrame(dict(x=[0,0,1,0,1], y=[1,0,1,1,0], z=[0,0,1,0,1]))
In [3]: df
Out[3]:
x y z
0 0 1 0
1 0 0 0
2 1 1 1
3 0 1 0
4 1 0 1
In [4]: df = df.drop(['x','y'], axis=1)
In [5]: df
Out[5]:
z
0 0
1 0
2 1
3 0
4 1
这篇关于Python Pandas-在一个命令中从数据框中删除多个序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!