Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将包含多行字符串的系列行拆分为单独的行_Python_Pandas_Split_Series - Fatal编程技术网

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')])