简体   繁体   中英

I have rows of strings in the MongoDB format in a specific column in the .csv file How do I convert it into dataframe?

列中的行类型为:{ "a" : "1", "b" : "2" }, { "a" : "1", "b" : "2", "c": "3 " } 等。如何将其转换为具有适当标签 a、b、c 的数据帧?

You need convert strings to dict s by json.loads and then call DataFrame contructor:

df = pd.DataFrame({'a': ['{ "a" : "1", "b" : "2" }', 
                         '{ "a" : "1", "b" : "2", "c": "3" }']})

df = pd.DataFrame(df['a'].apply(pd.io.json.loads).values.tolist())

print (df)
   a  b    c
0  1  2  NaN
1  1  2    3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM