Lfm_
Lfm_

Reputation: 107

Pandas str.split(' ') returning NaN

I have the following DataFrame created from a dictionary:

                                                     clusters
OG1.5_1000  [6243|g1697.t1_CBS136243, 6243|g7411.t1_CBS136...
OG1.5_1001  [2003|g3159.t1_CBS132003, 2003|g4503.t1_CBS132...
OG1.5_1002  [4916|g1071.t1_CBS134916, 4916|g1248.t1_CBS134...
OG1.5_1003  [4916|g913.t1_CBS134916, 4920|g2467.t1_CBS1349...
OG1.5_1004  [2003|g2248.t1_CBS132003, 2003|g3254.t1_CBS132...
OG1.5_1005  [2003|g1615.t1_CBS132003, 2003|g1622.t1_CBS132...

When I try to split using "," as delimiter I get as result multiple "NaN"

df['clusters'].str.split(',')

OG1.5_1001    NaN
OG1.5_1002    NaN
OG1.5_1003    NaN
OG1.5_1004    NaN
OG1.5_1005    NaN

Any suggestions on what I'm doing wrong? or How I could split the column "clusters"?

Upvotes: 4

Views: 7851

Answers (1)

jezrael
jezrael

Reputation: 863401

I think need DataFrame contructor, because in clusters are lists:

#if one column DataFrame
df = pd.DataFrame(df['clusters'].values.tolist(), index=df.index)

#if multiple columns and need join new df to existing
df = df.join(pd.DataFrame(df.pop('clusters').values.tolist(), index=df.index))

Upvotes: 5

Related Questions