用这个:
dataset = pd.read_csv('lyrics.csv', delimiter = '\t', quoting = 3)
我以这种方式打印我的数据集:
lyrics,classification
0 "I should have known better with a girl like you
1 That I would love everything that you do
2 And I do, hey hey hey, and I do
3 Whoa, whoa, I
4 Never realized what I kiss could be
5 This could only happen to me
6 Can't you see, can't you see
7 That when I tell you that I love you, oh
8 You're gonna say you love me too, hoo, hoo, ho...
9 And when I ask you to be mine
10 You're gonna say you love me too
11 So, oh I never realized what I kiss could be
12 Whoa whoa I never realized what I kiss could be
13 You love me too
14 You love me too",0
但我真正需要的是每行 "" 之间的所有内容。我如何在 pandas 中进行这种转换?
请您参考如下方法:
适用于 OP 的解决方案(来自评论):
从源头上解决问题(在 read_csv 中):
@nbeuchat is probably right, just try
dataset = pd.read_csv('lyrics.csv', quoting = 2)That should give you a dataframe with one row and two columns: lyrics (with embedded line returns in the string) and classification (0).
折叠系列字符串的一般解决方案:
您想使用 pd.Series.str.cat :
import pandas as pd
dataset = pd.DataFrame({'lyrics':pd.Series(['happy birthday to you',
'happy birthday to you',
'happy birthday dear outkast',
'happy birthday to you'])})
dataset['lyrics'].str.cat(sep=' / ')
# 'happy birthday to you / happy birthday to you / happy birthday dear outkast / happy birthday to you'
默认的 sep 是 None,它会给你 'happy birthday to youhappy birthday to youhappy ...' 所以选择 sep 适合您的值。上面我使用了斜线(用空格填充),因为这是您通常在歌曲和诗歌的引文中看到的。
您也可以尝试 print(dataset['lyrics'].str.cat(sep='\n')) 保留换行符但将它们全部存储在一个字符串中而不是一个字符串中每行字符串。
