亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何拆分字符串并將所有拆分添加到一個長列中?

如何拆分字符串并將所有拆分添加到一個長列中?

慕標琳琳 2023-10-26 10:50:32
我有一個包含一列和多行的數據框。每行包含一首歌曲的歌詞,行由“\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 列數據幀,其中歌詞已被刪除并被“[]”包圍。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 行,將相同的歌詞附加到該數據幀。謝謝!
查看完整描述

3 回答

?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

嘗試:

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


查看完整回答
反對 回復 2023-10-26
?
森林海

TA貢獻2011條經驗 獲得超2個贊

我從你的帖子中得知,歌詞df1只是一長串,而不是實際的list?如果是這種情況,那么我只需使用內置字符串方法將該split字符串用逗號連接起來,然后重新組裝成數據幀:


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].split(', ')如果您知道每首歌詞之間總是有一個逗號+一個空格,您也可以這樣做。


如果您的完整歌詞是 的一部分df1,您可以loc(或w/e)訪問該字符串并遵循此答案。


查看完整回答
反對 回復 2023-10-26
?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

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


查看完整回答
反對 回復 2023-10-26
  • 3 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號