[英]How to convert series to dataframe if the series is separated by comma using python?
Converting series to dataframe: 将系列转换为数据框:
I tried this: 我尝试了这个:
df = pd.Series(data)
df1=pd.DataFrame(df,sep=",")
print(df1)
This is the series format: 这是系列格式:
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())
since the series is in a list format.This line works. 由于该系列采用列表格式,因此此行有效。
Output: 输出:
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.