我有下面的 stories_data 字典,我可以從中創建一個 df 但因為owner它也是一個字典,所以我想獲取該字典的值,所以所有者列會有178413540
import numpy as np
import pandas as pd
stories_data = {'caption': 'Tel_gusto', 'like_count': 0, 'owner': {'id': '178413540'}, 'headers': {'Content-Encoding': 'gzip'}
x = pd.DataFrame(stories_data.items())
x.set_index(0, inplace=True)
stories_metric_df = x.transpose()
del stories_metric_df['headers']
我已經嘗試過了,但它得到的是關鍵而不是價值
stories_metric_df['owner'].explode().apply(pd.Series)
uj5u.com熱心網友回復:
您可以使用.str,甚至用于物件/字典:
stories_metric_df['owner'] = stories_metric_df['owner'].str['id']
輸出:
>>> stories_metric_df
0 caption like_count owner
1 Tel_gusto 0 178413540
另一種解決方案是跳過explode, 并提取id:
stories_metric_df['owner'].apply(pd.Series)['id']
雖然我懷疑我的第一個解決方案會更快。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422936.html
標籤:
