Python 选择dataframe列而不删除标签
如何选择数据帧列df['col'],而不删除列的名称Python 选择dataframe列而不删除标签,python,pandas,Python,Pandas,如何选择数据帧列df['col'],而不删除列的名称 df index colname col1 col2 col3 1 0 1 2 2 3 4 5 3 6 7 8 4 9 10 11 期望输出: df['col1'] index colname col1 1 0 2 3
df
index colname col1 col2 col3
1 0 1 2
2 3 4 5
3 6 7 8
4 9 10 11
期望输出:
df['col1']
index colname col1
1 0
2 3
3 6
4 9
编辑:正如正确回答的那样,df['col1']]完成任务。。。现在有点棘手了。如果列是多索引的呢
df grpname A B ... Z
index colname cA1 ... cAN cB1 ... cBN ... cZ1 ... cZN
1 a11 ... a1N b11 ... b1N ... z11 ... z1N
2 a21 ... a2N b21 ... b2N ... z21 ... z2N
3 a31 ... a3N b31 ... b3N ... z31 ... z3N
4 a41 ... a4N b41 ... b4N ... z41 ... z4N
我想去
df grpname A
index colname cA1 cA2
1 a11 a12
2 a21 a22
3 a31 a32
4 a41 a42
看起来.xs只允许我检索某个列,即df.xs'a',cAi',level='grpname',colname',axis=1,drop_level=False,df['a']['cA1':'cAi']也不起作用?对于单个列选择,df['col']将返回一个序列,如果要保留列名,则需要双下标,该下标将返回数据帧:
In [2]:
import pandas as pd
pd.set_option('display.notebook_repr_html', False)
import io
temp = """index col1 col2 col3
1 0 1 2
2 3 4 5
3 6 7 8
4 9 10 11"""
df = pd.read_csv(io.StringIO(temp), sep='\s+',index_col=[0])
df
Out[2]:
col1 col2 col3
index
1 0 1 2
2 3 4 5
3 6 7 8
4 9 10 11
In [4]:
df[['col1']]
Out[4]:
col1
index
1 0
2 3
3 6
4 9
与此形成对比的是:
In [5]:
df['col1']
Out[5]:
index
1 0
2 3
3 6
4 9
Name: col1, dtype: int64
编辑
正如@joris所指出的,您可以看到名称显示在输出的底部,因此名称不会丢失,只是一个不同的输出如果您确定每列占用的空间,有一种方法可以做到这一点。 这是
df[['col']]是否不符合您的要求?df[['col']]仍应有效我不完全理解您的要求,您能否发布代码以复制数据和输出现在可能更清楚了。我想得到只包含两个“col”的“A”组,而不删除标签。请发布复制数据和组的代码注意列名不是“丢失”,区别只是表示形式不同。该系列仍然有名称,但在表示中,这是在底部。谢谢,我没有想到它。。。您可以检查编辑,看看是否可以回答多索引列的问题吗?这不是关于读取文件,而是关于索引现有数据帧
np.loadtxt("df.txt",
dtype={
'names': ('index', 'colname', 'col1', 'col2', 'col3'),
'formats': (np.float, np.string, np.float, np.float, np.float)},
delimiter= ' ', skiprows=1)