[英]Reading dataframe and converting to text file
我正在處理一些使用 Pandas 從 csv 導入到數據框的數據集。 問題是我認為有一列看起來像字典。 “JUPYTER 內聯輸出”:
region_shape_attributes
0 {"name":"point","cx":1,"cy":2}
1 {"name":"point","cx":11,"cy":22}
2 {"name":"point","cx":111,"cy":222}
3 {"name":"point","cx":1111,"cy":2222}
如何遍歷上述數據框並獲取要存儲在文本文件中的 cx 和 cy 值。 “ 文本文件”
1 11 111 1111
2 22 222 2222
任何幫助真的很感激。 謝謝
這是執行此操作的代碼。 請注意,僅當region_shape_attributes
作為字符串讀取時才需要第一行。 如果它已經是字典,則不需要。
df["region_shape_attributes"] = df.region_shape_attributes.apply(json.loads)
pd.json_normalize(df.region_shape_attributes)[["cx", "cy"]].transpose()
結果是:
0 1 2 3
cx 1 11 111 1111
cy 2 22 222 2222
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.