使用pandas将.txt文件分为两列

使用pandas将.txt文件分为两列,pandas,csv,export-to-csv,data-cleaning,Pandas,Csv,Export To Csv,Data Cleaning,我有一个脚本的文本文件,其顺序如下: 0 "character one" "dialogue for character one." 1 "character two" "dialogue for character two." 2 "character one" "dialogue for character one again" ... etc 我的问题是,我想分析这个文本,需要它是.csv格式,其中字符在第一列,而对话都在第二列 我已将.txt文件读入熊猫,如下所示: txt\u ep

我有一个脚本的文本文件,其顺序如下:

0 "character one" "dialogue for character one."
1 "character two" "dialogue for character two." 
2 "character one" "dialogue for character one again"
...
etc
我的问题是,我想分析这个文本,需要它是.csv格式,其中字符在第一列,而对话都在第二列

我已将.txt文件读入熊猫,如下所示:

txt\u ep\u 4=pd.read\u表('/Users/nathancahn/star\u wars/0\u data/ep\u IV\u script.txt)
所以现在我有了一个熊猫数据系列(不是一个数据框架)来进行交互

我主要尝试了使用Series.str.split()将文本拆分为列的不同方法,但都没有成功。我使用
series\u txt\u ep\u 4.str.split(pat=“”)
表示在空格处分开,但这表示在每个空格处分开

同样,我的理想输出是第一列是字符名,第二列是与该字符相关联的对话字符串。

我认为您需要使用参数
sep
names
来获得新的列名,因为在
pandas 0.24.2
中:

未来警告:read_表已弃用,请改用read_csv

编辑:

如果值也有标题:

temp=u""""character" "dialogue"
"1" "THREEPIO" "Did you hear that?  They've shut down the main reactor.  We'll be destroyed for sure.  This is madness!"
"2" "THREEPIO" "We're doomed!"
"3" "THREEPIO" "There'll be no escape for the Princess this time."
"4" "THREEPIO" "What's that?"
"5" "THREEPIO" "I should have known better than to trust the logic of a half-sized thermocapsulary dehousing assister..."
"6" "LUKE" "Hurry up!  Come with me!  What are you waiting for?!  Get in gear!"
"7" "THREEPIO" "Artoo! Artoo-Detoo, where are you?"
"8" "THREEPIO" "At last!  Where have you been?"
"9" "THREEPIO" "They're heading in this direction. What are we going to do?  We'll be sent to the spice mines of Kessel or smashed into who knows what!"
"10" "THREEPIO" "Wait a minute, where are you going?"
"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), sep="\s+")

我相信对于新列名称,您需要使用参数
sep
names
,因为在
pandas 0.24.2
中获得:

未来警告:read_表已弃用,请改用read_csv

编辑:

如果值也有标题:

temp=u""""character" "dialogue"
"1" "THREEPIO" "Did you hear that?  They've shut down the main reactor.  We'll be destroyed for sure.  This is madness!"
"2" "THREEPIO" "We're doomed!"
"3" "THREEPIO" "There'll be no escape for the Princess this time."
"4" "THREEPIO" "What's that?"
"5" "THREEPIO" "I should have known better than to trust the logic of a half-sized thermocapsulary dehousing assister..."
"6" "LUKE" "Hurry up!  Come with me!  What are you waiting for?!  Get in gear!"
"7" "THREEPIO" "Artoo! Artoo-Detoo, where are you?"
"8" "THREEPIO" "At last!  Where have you been?"
"9" "THREEPIO" "They're heading in this direction. What are we going to do?  We'll be sent to the spice mines of Kessel or smashed into who knows what!"
"10" "THREEPIO" "Wait a minute, where are you going?"
"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), sep="\s+")


啊,有意思,这看起来是正确的方向。但我不确定我是否100%理解这段代码。那么这里的
temp
应该是我通过的pd.series吗?因为我在尝试将pd.series读入时出错。@NathanCahn-不,不要传递series,使用
txt\u ep_4=pd.read\u csv('/Users/NathanCahn/star\u wars/0\u data/ep_IV_script.txt',sep=“\s+”,names=['a','b'])
啊,好的,似乎给出了一个在“字符”后截断的熊猫数据帧现在不包括对话。我现在意识到这可能是因为我的.txt文件实际上在“character”列之前包含一个索引号。像这样:``“1”“字符”“对话”“2”“字符”“对话”等``@NathanCahn-数据是否机密?@NathanCahn-因为最好是使用文件,如果一些引用和类似,如果可能将值粘贴到问题,则应更改一些值。那么,是否可以将文件的前5行上载到gdocs、dropbox或类似文件,并共享链接?啊,有趣的是,这看起来是正确的方向。但我不确定我是否100%理解这段代码。那么这里的
temp
应该是我通过的pd.series吗?因为我在尝试将pd.series读入时出错。@NathanCahn-不,不要传递series,使用
txt\u ep_4=pd.read\u csv('/Users/NathanCahn/star\u wars/0\u data/ep_IV_script.txt',sep=“\s+”,names=['a','b'])
啊,好的,似乎给出了一个在“字符”后截断的熊猫数据帧现在不包括对话。我现在意识到这可能是因为我的.txt文件实际上在“character”列之前包含一个索引号。像这样:``“1”“字符”“对话”“2”“字符”“对话”等``@NathanCahn-数据是否机密?@NathanCahn-因为最好是使用文件,如果一些引用和类似,如果可能将值粘贴到问题,则应更改一些值。那么,是否可以将文件的前5行上载到gdocs、dropbox或类似文件,并共享链接?
print (df)

   character                                           dialogue
1   THREEPIO  Did you hear that?  They've shut down the main...
2   THREEPIO                                      We're doomed!
3   THREEPIO  There'll be no escape for the Princess this time.
4   THREEPIO                                       What's that?
5   THREEPIO  I should have known better than to trust the l...
6       LUKE  Hurry up!  Come with me!  What are you waiting...
7   THREEPIO                 Artoo! Artoo-Detoo, where are you?
8   THREEPIO                     At last!  Where have you been?
9   THREEPIO  They're heading in this direction. What are we...
10  THREEPIO                Wait a minute, where are you going?