簡體   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