我从字典创建了以下 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

关于我做错了什么的任何建议?或者我如何拆分“集群”列?

最佳答案

我认为需要 DataFrame 构造函数,因为在 clusters 中有 list s:

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

关于python - Pandas str.split (' ' ) 返回 NaN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50529269/

10-12 19:06