簡體   English   中英

如何訪問 pandas dataframe 列中的字典元素並遍歷它們並創建填充各自值的新列?

[英]How to acesses dictionary elements within a pandas dataframe column and iterate over them too create new columns filled with their respective values?

我有一個 pandas dataframe 和一個包含字典的列,我想遍歷字典鍵以創建以各種鍵命名的新列。 當我嘗試獲取嵌套列表邏輯以使用“標簽”列中的相應鍵值填充這些行值時,問題就出現了。 現在我的代碼生成列名,但用相同的值填充所有行。 我以為 get() 應該在每次迭代時檢索相應的值?

for x in df1['tags']:
    for e in x.values():
        df1[str(e)] = x.get(e)

列“標簽”

for i, row in df1.iterrows():
   for e in row['tags']:
      df1.loc[i, e] = row['tags'][e]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM