繁体   English   中英

如果使用python将序列用逗号分隔,如何将序列转换为数据帧?

[英]How to convert series to dataframe if the series is separated by comma using python?

将系列转换为数据框:

我尝试了这个:

df = pd.Series(data)
df1=pd.DataFrame(df,sep=",")
print(df1)

这是系列格式:

0     (The Associated Press, Shock in Japan as Nissa...
1     (The Associated Press, Shock in Japan as Nissa...
2     (The Associated Press, Venezuelan ambassador t...
3     (None, WATCH: World News 11/19/18: Deadly Shoo...
4     (The Associated Press, AP Photos: Northeastern...
5     (The Associated Press, Esi Edugyan wins prize ...
6     (The Associated Press, China's Xi visits Manil...
pd.DataFrame(df.tolist())

由于该系列采用列表格式,因此此行有效。

输出:

                                         0                          1
0                     The Associated Press  Shock in Japan as Nissan's Ghosn held in..

1                     The Associated Press  Shock in Japan as Nissan's Ghosn held.. 

2                     The Associated Press   Venezuelan ambassador to Cuba Ali Rodriguez..

3                                     None  WATCH: World News 11/19/18: Deadly Shooting...
df1 = pd.DataFrame(data)
df1 = df1.replace({'(': '', ')': ''})
df1 = df1[0].str.split(',',expand=True)

df1 = df1.rename(columns={0: 'col1', 1: 'col2'})

print(df1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM