基本上,我认为解释这一点的最简单方法是,我试图扩展一个多索引表,但索引都在同一列中。
我的数据结构如下:
| 总和| Sum |
| --| ------------ |
| 二十二| 22 |
| 10个| 10 |
| 十二岁| 12 |
| 三十三| 33 |
| 三十三| 33 |
| 四十五| 45 |
| 十四岁| 14 |
| 三十一| 31 |
| 一百| 100 |
我想要的是这样的Dataframe:
| 数据1|数据2|总和| Sum |
| --|--|--| ------------ |
| 10个|十二岁|二十二| 22 |
| 三十三|0|三十三| 33 |
| 十四岁|三十一|四十五| 45 |
| 五十七|四十三|一百| 100 |
有没有内置的pandas方法,或者直接的方法来处理这种类型的翻译?
我试过手动分解表并重新创建它,方法是收集重复的行标签,并从这些标签中创建列,使用带有该标签的行的数据,但是,棘手的地方是缺少子数据的地方;就像上面的例子,Collection 2 Data 2不存在。使用这种方法,我可以计算每行Data 1是否等于Collection 1,如果是,在该索引处将0添加到Data 2。但是,它似乎超级丑陋,并认为可能有一个更优雅的方法。
4条答案
按热度按时间g2ieeal71#
使用
pivot_table
:字符串
输出量:
型
qf9go6mv2#
我不确定是否存在一些简单的Pandas解决方案,但你可以试试这个例子:
字符串
图纸:
型
yqyhoc1h3#
欢迎来到SO。在我看来,最简单的台词是:
输入数据:
字符串
(1)重新制定表格,以便它可以旋转
(解析 * 收集 * 和 * 数据 * 信息)
型
重新制定的输入表:
型
(2)透视,然后用两个方向的总和完成表格:
型
最终输出:
型
cngwdvgl4#
另一种可能的解决方案:
字符串
输出量:
型