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?
答案 0 :(得分:0)
首先你不能拆分,因为Series
的值是tuple
和None
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