Column splitting issue Python

时间:2018-02-03 11:14:13

标签: python pandas dataframe

I'm trying to split data in Series format into DataFrame

12                                                 None
13                                 (Nokia X3, 62, 1293)

using command:

pd.DataFrame(matching_results.str.split(',')
         ,columns="Model Match_Score Id".split())

But, recieving just empty dataframe

    Model   Match_Score     Id

​Because "core", e.g. matching_results.str.split(',') returns:

12      NaN
13      NaN

expand = True doesn't solve it.

Where am I wrong?

1 个答案:

答案 0 :(得分:0)

首先你不能拆分,因为Series的值是tupleNone s

有点复杂,因为None s,所以先按dropna删除它们,使用DataFrame构造函数,然后按reindex添加NaN s :

matching_results = pd.Series([None, ('Nokia X3', 62, 1293), None], index=[12,13,14])
print (matching_results)
12                    None
13    (Nokia X3, 62, 1293)
14                    None
dtype: object

new = matching_results.dropna()
df = pd.DataFrame(new.values.tolist(),
                  columns="Model Match_Score Id".split(),
                  index=new.index).reindex(matching_results.index)
print (df)
       Model  Match_Score      Id
12       NaN          NaN     NaN
13  Nokia X3         62.0  1293.0
14       NaN          NaN     NaN