Pandas:摆脱多索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44023770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: Get rid of multiindex
提问by ScientiaEtVeritas
After grouping and counting I'm trying to remove the multiindex like this:
分组和计数后,我试图删除这样的多索引:
df = df[['CID','FE', 'FID']].groupby(by=['CID','FE']).count()
.unstack().reset_index()
Printing the columns (df.colums
) show that it is still a MultiIndex.
打印列 ( df.colums
) 表明它仍然是一个 MultiIndex。
MultiIndex(levels=[['FID', 'CID'], [...]]
MultiIndex(levels=[['FID', 'CID'], [...]]
I can't access the column CID
via df['CID']
我无法CID
通过以下方式访问该列df['CID']
回答by jezrael
I think you need if is necessary convert MultiIndex
to Index
:
我认为如果有必要,您需要转换MultiIndex
为Index
:
df.columns = df.columns.map(''.join)
Or if need remove level use droplevel
:
或者如果需要删除级别使用droplevel
:
df.columns = df.columns.droplevel(0)
If need access to values is possible use xs
:
如果需要访问值是可能的使用xs
:
df = df.xs('CID', axis=1, level=1)
You can also check:
您还可以检查:
What is the difference between size and count in pandas?
EDIT:
编辑:
For remove MultiIndex is another solution select by ['FID']
.
对于 remove MultiIndex 是另一个解决方案 select by ['FID']
。
df = df.groupby(by=['CID','FE'])['FID'].count().unstack().reset_index()
Samples (also added rename_axis
for nicer output):
示例(还添加rename_axis
了更好的输出):
df = pd.DataFrame({'CID':[2,2,3],
'FE':[5,5,6],
'FID':[1,7,9]})
print (df)
CID FE FID
0 2 5 1
1 2 5 7
2 3 6 9
df = df.groupby(by=['CID','FE'])['FID']
.count()
.unstack()
.reset_index()
.rename_axis(None, axis=1)
print (df)
CID 5 6
0 2 2.0 NaN
1 3 NaN 1.0
回答by Allen
This should get rid of MultiIndex for CID and allow you to access it via df['CID']
这应该摆脱 CID 的 MultiIndex 并允许您通过 df['CID'] 访问它
df = df.rename(columns={('CID',''):'CID'})