Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas str.split(' ') returning NaN

Tags:

python

pandas

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"?

like image 968
Lfm_ Avatar asked Oct 23 '25 23:10

Lfm_


1 Answers

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))
like image 180
jezrael Avatar answered Oct 25 '25 13:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!