如有必要,您可以先将Json Column转换为dictjson.loads:
import json
df = pd.DataFrame({'Column 1':[123],
'Column 2':['ABC'],
'Json Column':['{"anotherNumber":345,"anotherString":"DEF"}']})
print (df)
Column 1 Column 2 Json Column
0 123 ABC {'anotherString': 'DEF', 'anotherNumber': 345}
print (type(df.ix[0,'Json Column']))
<class 'str'>
df['Json Column'] = df['Json Column'].apply((json.loads))
print (type(df.ix[0,'Json Column']))
<class 'dict'>
然后生成列表列表并从构造函数创建Dataframe:
print (df['Json Column'].values.tolist())
[{'anotherString': 'DEF', 'anotherNumber': 345}]
df1 = pd.DataFrame(df['Json Column'].values.tolist())
print (df1)
anotherNumber anotherString
0 345 DEF
最后一个 concat 到原始,其中列 Json Column 被 drop 删除:
print (pd.concat([df.drop('Json Column', axis=1), df1], axis=1))
Column 1 Column 2 anotherNumber anotherString
0 123 ABC 345 DEF