Python 如何从数据框中删除\n并将数据移动到新行
我有一个熊猫数据框,看起来像这样Python 如何从数据框中删除\n并将数据移动到新行,python,pandas,Python,Pandas,我有一个熊猫数据框,看起来像这样 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 Index column1
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
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
我想删除\n并将剩余内容移动到下一个位置。因此,我需要这样的数据帧
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
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.replaceto|u 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}}
一种方法是定义一个函数来展平列:
from itertools import chain
def flatten(col):
return list(chain.from_iterable([i for i in col.str.split(r" \\n") if i]))
df[["column2","column3"]] = df[["column2","column3"]].apply(flatten)
print (df)
Index column1 column2 column3
0 0 3 2 8
1 1 7 89 56
2 2 8 6 4
编辑:使用新的示例数据,这里有一个更新的方法:
def flatten(col):
return [i for i in chain.from_iterable(col.str.split(r" \n")) if i]
print (df.astype(str).apply(flatten))
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
使用:
这张照片是:
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
一种使用和
你好,Henry,很抱歉,我收到以下错误类型错误:“float”对象不可编辑,“发生在index column2'@hargurjeetsingh,因此这意味着您的实际数据与您提供的示例不同。我尝试在样本数据本身而不是实际数据上运行代码。下面一行df[[column2,column3]]=df[[column2,column3]]出现错误。ApplyFlatentry做一个df.to_dict并将其添加到您的问题中。执行代码后,我得到这个输出列1[3,9,8,4,9,78]column2[2,89,6,12,12,68]column3[8,56,4,32,5,56]如何将其放入df并删除空值。Hi@Shubham,谢谢你的回复。这确实很有效,但并不完全有效。虽然89和56将移到下一行,但r刚好低于89和56的整数“6”和“4”将被“空白”所取代,您可以共享更大的数据帧样本吗?@HargurJeethigh使用此数据帧更新您的问题。如建议的……将数据帧更新为question@jezrael是的,谢谢:@jcaiz,谢谢你的回复。虽然89和56将移到下一行,但89和56以下的“6”和“4”将被NaN替换。@hargurjeetsingh请提供更多示例,并确保是真实的测试用例。更新了问题中的数据框。希望对我编辑问题有所帮助。你仍然可以有两种验证方法,很高兴看到这一点。您的实际数据与您最初提供的数据大不相同。我已经编辑了我的答案。