简体   繁体   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?

I have a pandas dataframe with a column that contains a dictionary, I'd like to iterate over the dictionary keys to create new columns named after the various keys.我有一个 pandas dataframe 和一个包含字典的列,我想遍历字典键以创建以各种键命名的新列。 The issue come whens I try to get the nested list logic to fill those row values with the corresponding key values from the 'tags' column.当我尝试获取嵌套列表逻辑以使用“标签”列中的相应键值填充这些行值时,问题就出现了。 Right now the code I have generates the column names, but fills all the rows with the same values.现在我的代码生成列名,但用相同的值填充所有行。 I thought.get() was supposed to retrieve the corresponding value on each iteration?我以为 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.

相关问题 如何遍历熊猫数据框并创建新列 - How to iterate over pandas dataframe and create new column 根据两个不同列中的各自值在 DataFrame 中创建新列 - Create new column in DataFrame based on respective values in two different columns 迭代 Pandas 数据框中的多列并创建新列 - Iterate over multiple columns in Pandas dataframe and create new columns 如何根据数据框中列的值在 Pandas 中创建新列(pivot 或 unpivot ?) - how to create new columns in pandas based on a column's values within a dataframe (pivot or unpivot ? ) 如何迭代 Pandas 中的列值并根据同一行中多列的值创建新的观察? - How can I iterate over column values in Pandas and create a new observation based on the values of multiple columns in the same row? 遍历 Pandas dataframe 的列并创建新变量 - Iterate over columns of Pandas dataframe and create new variables 遍历 pandas 数据框中的行并匹配列表字典中的值以创建新列 - Iterate through rows in pandas dataframe and match values in a dictionary of lists to create a new column 遍历 pandas 列中的字典列表并创建新列 - Iterate over a list of dictionaries in pandas column and create new columns 如何迭代列中的值并将它们添加为新列? - How to iterate over value in a column and add them as new columns? 如何迭代 Pandas 数据框列中的元素? - How do I iterate over elements in a pandas dataframe column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM