Rob*_*ith 11 python hierarchical pandas
另一个熊猫问题:
我有这个表与层次索引:
In [51]:
from pandas import DataFrame
f = DataFrame({'a': ['1','2','3'], 'b': ['2','3','4']})
f.columns = [['level1 item1', 'level1 item2'],['', 'level2 item2'], ['level3 item1', 'level3 item2']]
f
Out[51]:
level1 item1 level1 item2
level2 item2
level3 item1 level3 item2
0 1 2
1 2 3
2 3 4
Run Code Online (Sandbox Code Playgroud)
选择level1 item1产生以下错误:
In [58]: f['level1 item1']
AssertionError: Index length did not match values
Run Code Online (Sandbox Code Playgroud)
但是,这似乎与级别数有些相关.当我将级别数减少到只有两个时,没有这样的错误:
from pandas import DataFrame
f = DataFrame({'a': ['1','2','3'], 'b': ['2','3','4']})
f.columns = [['level1 item1', 'level1 item2'],['', 'level1 item2']]
f
Out[59]:
level1 item1 level1 item2
level1 item2
0 1 2
1 2 3
2 3 4
Run Code Online (Sandbox Code Playgroud)
相反,之前的DataFrame提供了预期的系列:
In [63]:
f['level1 item1']
Out[63]:
0 1
1 2
2 3
Name: level1 item1
Run Code Online (Sandbox Code Playgroud)
level1 item1用虚拟角色填补下面的空白"修复"了这个问题,但这不是一个好的解决方案.
如何在不使用虚拟名称填充这些列的情况下解决此问题?
非常感谢!

该表是使用以下索引生成的:
index = [np.array(['Size and accumulated size of adjusted gross income', 'All returns', 'All returns', 'All returns', 'All returns', 'All returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns']),
np.array(['', 'Number of returns', 'Percent of total', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit', 'Number of returns', 'Percent of total', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit', 'Taxable income', 'Taxable income', 'Taxable income', 'Income tax after credits', 'Income tax after credits', 'Income tax after credits', 'Total income tax', 'Total income tax', 'Total income tax', 'Total income tax', 'Total income tax']),
np.array(['', '', '', '', '', '', '', '','', '', 'Number of returns', 'Amount', 'Percent of total', 'Number of returns', 'Amount', 'Percent of total', 'Amount', 'Percent of', 'Percent of', 'Percent of', 'Average total income tax (dollars)']),
np.array(['', '', '', 'Amount', 'Percent of total', 'Average (dollars)', 'Average (dollars)', 'Average (dollars)', 'Amount', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Total', 'Taxable income', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit'])]
df.columns = index
Run Code Online (Sandbox Code Playgroud)
这是CSV文件中某些数据的近乎完美的副本,但您可以看到在"返回数","总数百分比"和"调整后的总收入减去赤字"之下存在差距.当我尝试选择返回数时,该差距会产生此错误:
In [68]: df['Taxable returns']['Number of returns']
AssertionError: Index length did not match values
Run Code Online (Sandbox Code Playgroud)
我不明白这个错误.所以一个很好的解释将受到高度赞赏.在任何情况下,当我使用此索引填充该间隙时(注意第三个numpy数组中的第一个元素):
index = [np.array(['Size and accumulated size of adjusted gross income', 'All returns', 'All returns', 'All returns', 'All returns', 'All returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns', 'Taxable returns']),
np.array(['', 'Number of returns', 'Percent of total', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit', 'Number of returns', 'Percent of total', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit', 'Taxable income', 'Taxable income', 'Taxable income', 'Income tax after credits', 'Income tax after credits', 'Income tax after credits', 'Total income tax', 'Total income tax', 'Total income tax', 'Total income tax', 'Total income tax']),
np.array(['1', '2', '3', '4', '5', '6', '7', '8','9', '10', 'Number of returns', 'Amount', 'Percent of total', 'Number of returns', 'Amount', 'Percent of total', 'Amount', 'Percent of', 'Percent of', 'Percent of', 'Average total income tax (dollars)']),
np.array(['', '', '', 'Amount', 'Percent of total', 'Average (dollars)', 'Average (dollars)', 'Average (dollars)', 'Amount', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Percent of total', 'Total', 'Taxable income', 'Adjusted gross income less deficit', 'Adjusted gross income less deficit'])]
df.columns = index
Run Code Online (Sandbox Code Playgroud)
我得到了正确的结果:
In [71]: df['Taxable returns']['Number of returns']
Out[71]:
7
Average (dollars)
0 90,660,104
1 3,495
...
Run Code Online (Sandbox Code Playgroud)
我昨天推了修这个.这是github master上的新行为:
In [1]: paste
from pandas import DataFrame
f = DataFrame({'a': ['1','2','3'], 'b': ['2','3','4']})
f.columns = [['level1 item1', 'level1 item2'],['', 'level2 item2'], ['level3 item1', 'level3 item2']]
f
## -- End pasted text --
Out[1]:
level1 item1 level1 item2
level2 item2
level3 item1 level3 item2
0 1 2
1 2 3
2 3 4
In [2]: f['level1 item1']
Out[2]:
level3 item1
0 1
1 2
2 3
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1630 次 |
| 最近记录: |