简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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