Pandas str.split('')返回NaN

时间:2018-05-25 12:29:20

标签: python pandas

我从字典中创建了以下DataFrame:

                                                     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...

当我尝试使用""作为分隔符,我得到多个" 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

对我做错了什么的任何建议?或者我如何拆分列"集群"?

1 个答案:

答案 0 :(得分:1)

我认为需要DataFrame个构造函数,因为在clusterslist是:{/ p>

#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))