Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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_Dataframe - Fatal编程技术网

Python 如何拆分字符串并将所有拆分内容添加到一个长列中?

Python 如何拆分字符串并将所有拆分内容添加到一个长列中?,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个一列多行的数据框。每一行包含一首歌曲的歌词,歌词行由“\n”分隔,到目前为止,我得到的是 with open('Lyrics_Pavement.json') as json_data: data = json.load(json_data) df = pd.DataFrame(data['songs']) df1 = df.lyrics.str.split(pat="\n") 然后,df1包含一个1列数据帧,其中的歌词已被删除,\n并被“[]”包围 1 [It

我有一个一列多行的数据框。每一行包含一首歌曲的歌词,歌词行由“\n”分隔,到目前为止,我得到的是

with open('Lyrics_Pavement.json') as json_data:
data = json.load(json_data)
df = pd.DataFrame(data['songs'])
df1 = df.lyrics.str.split(pat="\n")
然后,df1包含一个1列数据帧,其中的歌词已被删除,\n并被“[]”包围

1    [It's the shouting, it's the shouting, It's the Dutchman, it's the Dutchman shout, Get it away, I don't need your shaft, It's the shouting, it's the shouting, It's the shouting, it's the Dutchman shout, Give it away, I don't need your shaft, (yes I do), It's the shouting, it's the shouting, It's the shouting, it's the Dutchman shout, Get it away, I don't need your shaft] 
这是第1行的一个示例。我如何使数据显示为这样:

It's the shouting,
It's the shouting,
It's the dutchman
等等。上面的每一行都是数据帧的一行。然后,对于第2行,将这些相同的歌词附加到该数据帧

谢谢

编辑: 原始数据如下所示

    0                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
1  It's the shouting, it's the shouting\nIt's the Dutchman, it's the Dutchman shout\nGet it away, I don't need your shaft\nIt's the shouting, it's the shouting\nIt's the shouting, it's the Dutchman shout\nGive it away, I don't need your shaft\n(yes I do)\nIt's the shouting, it's the shouting\nIt's the shouting, it's the Dutchman shout\nGet it away, I don't need your shaft                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
2  Everybody's going out tonight\nAnd everybody's hanging out tonight, it's alright\nListen to them show when they learn how to lie\nYou can sing along with them if you try\nHolding the spoken world up\nI need it, I need it, I need it\nShe's got a neck like a Saturday paper\nYou read it fast before the Sunday mass\nTake it back it's nothing\nIt's nothing like it talks\nDon't listen to me baby if you wanna survive\nYou did it, you did it, you did it wrong\nCollapse, it's evident to me\nTonight all the friends are strangers\nI'd like to take my story\nTo some place the don't know me\nCreatures come out of the bars\nTalk is small but you talk to Paul\nSold sex on Bird Cage walk\nBoys are drinking and on the docks\nUnion man, I'm your friend\nI need you when you listen to me\nWeigh sand, Weigh sand\nBuy sand glass track\nClosing off, close off, close off\nOPEC dreams stock market close on the rock and roll scale stereo\nNo more partridge family dreams to the home land\nDivorce countries  
因此我可以替换“\n”上的拆分。那么我会使用for循环来附加每首额外的歌曲吗?第0行恰好是一首器乐歌曲

edit2:这给了我AttributeError:“list”对象没有属性“split”

with open('Lyrics_Pavement.json') as json_data:
    data = json.load(json_data)

df = pd.DataFrame(data['songs'])


df1 = df.lyrics.str.split(pat="\n")

s = df1.loc[1]
lines = [i.strip() for i in s[1:-1].split(',')]
df2 = pd.DataFrame(lines)

print(df2)

Edit3:上面的代码确实有效,我所指的数据帧是错误的。以下任一解决方案都有效,我将逗号上的分隔改为“\n”。谢谢大家

我从你的帖子中得知,
df1
中的歌词只是一个长字符串,而不是实际的
列表
?如果是这样的话,那么我只需要使用内置的string方法
将这个字符串用逗号拆分,然后重新组装成一个数据帧:

s = "[It's the shouting, it's the shouting, It's the Dutchman, it's the Dutchman shout, Get it away, I don't need your shaft, It's the shouting, it's the shouting, It's the shouting, it's the Dutchman shout, Give it away, I don't need your shaft, (yes I do), It's the shouting, it's the shouting, It's the shouting, it's the Dutchman shout, Get it away, I don't need your shaft]"

lines = [i.strip() for i in s[1:-1].split(',')]
df = pd.DataFrame(lines)
输出:

                          0
0         It's the shouting
1         it's the shouting
2         It's the Dutchman
3   it's the Dutchman shout
4               Get it away
5   I don't need your shaft
6         It's the shouting
7         it's the shouting
8         It's the shouting
9   it's the Dutchman shout
10             Give it away
11  I don't need your shaft
12               (yes I do)
13        It's the shouting
14        it's the shouting
15        It's the shouting
16  it's the Dutchman shout
17              Get it away
18  I don't need your shaft
  • s[1:-1]
    省略括号
  • .split(',')
    以逗号分隔
  • .strip()
    删除多余空格
如果您知道每首歌词之间总是只有一个逗号+一个空格,那么您也可以执行
lines=s[1:-1]。拆分(',')

如果您的完整歌词是
df1
的一部分,您只需
loc
(或w/e)访问字符串并按照此答案进行操作。

尝试:

import pandas as pd

longstring = '''It's the shouting, it's the shouting, It's the Dutchman, it's the Dutchman shout, Get it away, I don't need your shaft, It's the shouting, it's the shouting, It's the shouting, it's the Dutchman shout, Give it away, I don't need your shaft, (yes I do), It's the shouting, it's the shouting, It's the shouting, it's the Dutchman shout, Get it away, I don't need your shaft'''


splitstring = [e.strip()+"," for e in longstring.split(",")]
splitstring[-1] = splitstring[-1].replace(",","")
df1 = pd.DataFrame(splitstring)
print(df1)  


#                           0
#0         It's the shouting,
#1         it's the shouting,
#2         It's the Dutchman,
#3   it's the Dutchman shout,
#4               Get it away,
#5   I don't need your shaft,
#6         It's the shouting,
#7         it's the shouting,
#8         It's the shouting,
#9   it's the Dutchman shout,
#10             Give it away,
#11  I don't need your shaft,
#12               (yes I do),
#13        It's the shouting,
#14        it's the shouting,
#15        It's the shouting,
#16  it's the Dutchman shout,
#17              Get it away,
#18   I don't need your shaft

啊,与我的答案类似,我在主要帖子后面添加了逗号,以使文章更加清晰。当我试着这样做时,我得到了一个错误AttributeError:“list”对象没有属性“split”,我把我正在使用的代码放在主后处理中。事实上,我意识到我做错了什么,包括这个和其他解决方案。谢谢你们两位!
df1 = df.lyrics.str.split(pat="\n").explode()