我有一个熊猫数据框,看起来像这样
Index column1 column2 column3
0 3 \n9 2 \n89 8 \n56
1
2 8 6 4
3 4 \n9 12 \n12 32 \n5
4
5 78 68 56
我想去掉\n
,把剩下的内容移到下一个类似的地方。因此,我需要这样的数据帧
Index column1 column2 column3
0 3 2 8
1 9 89 56
2 8 6 4
3 4 12 32
4 9 12 5
5 78 68 56
我已经能够使用replace函数删除\n
df1.replace(to_replace=[r"\\t|\\n|\\r", "\t|\n|\r"], value=["",""], regex=True)
但我无法将整数值9,89,56移动到下一行。可能吗
样本数据:
{'column1': {0: '3 \\n9', 1: '', 2: 8, 3: '4 \\n9', 4: '', 5: 78},
'column2': {0: '2 \\n89', 1: '', 2: 6, 3: '12 \\n12', 4: '', 5: 68},
'column3': {0: '8 \\n56', 1: '', 2: 4, 3: '32 \\n5', 4: '', 5: 56}}
使用^{} 和^{} 的单行程序
一种方法是定义一个函数来展平列:
编辑:使用新的示例数据,这里有一个更新的方法:
使用:
这张照片是:
相关问题 更多 >
编程相关推荐