Python 将包含多行字符串的系列行拆分为单独的行
我有一个熊猫系列,里面有这样的字符串:Python 将包含多行字符串的系列行拆分为单独的行,python,pandas,split,series,Python,Pandas,Split,Series,我有一个熊猫系列,里面有这样的字符串: In: s = pd.Series(['This is a single line.', 'This is another one.', 'This is a string\nwith more than one line.']) Out: 0 This is a single line. 1 This is another one. 2 Thi
In:
s = pd.Series(['This is a single line.', 'This is another one.', 'This is a string\nwith more than one line.'])
Out:
0 This is a single line.
1 This is another one.
2 This is a string\nwith more than one line.
dtype: object
如何将此系列中包含换行符\n
的所有行拆分为它们自己的行?我所期望的是:
0 This is a single line.
1 This is another one.
2 This is a string
3 with more than one line.
dtype: object
我知道我可以用换行符拆分每一行
s = s.str.split('\n')
给
0 [This is a single line.]
1 [This is another one.]
2 [This is a string, with more than one line.]
但这只会将行内的字符串断开,而不会将每个标记分成自己的行。您可以循环每行中的每个字符串以创建新的序列:
pd.Series([j for i in s.str.split('\n') for j in i])
在输入上执行此操作可能比创建临时序列更有意义,例如:
strings = ['This is a single line.', 'This is another one.', 'This is a string\nwith more than one line.']
pd.Series([j for i in strings for j in i.split('\n')])