我在以下熊猫数据框中有一个索引,索引在左侧:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
A17 a b 1 AUG) NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
nn6 c d 2 POS) e f 2 Hi)
AZV NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
JFK a b 4 UUI) c v 8 Yo) t f 9 po)
我希望将其重新塑造为:
0 1 2 3
A17 a b 1 AUG)
nn6 c d 2 POS)
nn6 e f 2 Hi)
AZV NaN NaN NaN NaN
JFK a b 4 UUI)
JFK c v 8 Yo)
JFK t f 9 po
我尝试了reshape()
并使用itertools
对列进行迭代,但似乎仍然无法获得它
基本上,每次遇到a)时,都会切换到新行。 真正的表有150多列
谢谢
还有其他选择,如切片列和追加,但这非常简单
另一个不需要对行进行迭代的选项(如果有很多行,则迭代速度可能非常慢)是执行以下操作
然后把第4列放到第6列,这很简单。 我添加了一些输出,以便您可以看到每个步骤中发生了什么
我认为一种有效的方法是将数据帧分成4个相等的部分,然后沿着索引重新合并
这里的问题是我们可以在concat语句中动态重命名的列名
这里唯一的区别是,由于是na,您缺少所需输出中的一行
我们可以使用
combine_first
进行联合,以获得两个数据帧之间的增量相关问题 更多 >
编程相关推荐