简体   繁体   English

如何对列中的每个字典执行操作?

[英]How to perform operation over each dictionary in column?

If I have a column in dataframe with dictionaries:如果我在数据框中有一列带有字典:

col1
{10:24, 7:3}
{5:24, 1:2, 7:8}
{1:1}

How to perform operation of extracting keys from each dictionary for each rows?如何为每一行执行从每个字典中提取键的操作? So I need to get:所以我需要得到:

col1
10, 7
5, 1, 7
1

How to do that?怎么做? this df["col1"] = df["col1"].keys() doesnt work and I don't know why这个 df["col1"] = df["col1"].keys() 不起作用,我不知道为什么

DataFrame has .keys() to get own indexes, not to get keys from dictionares in cells. DataFrame.keys()来获取自己的索引,而不是从单元格中的字典中获取键。

But you can use .apply() to run function on every elemement in column separatelly.但是您可以使用.apply()分别在列中的每个.apply()上运行函数。

df['col1'] = df['col1'].apply(lambda item: item.keys())

Minimal working example:最小工作示例:

import pandas as pd

df = pd.DataFrame({'col1':[
   {10:24, 7:3},
   {5:24, 1:2, 7:8},
   {1:1},
]})


df['col1'] = df['col1'].apply(lambda item: item.keys())

print(df)

Result (now it has tuples with numbers):结果(现在它有带数字的元组):

        col1
0    (10, 7)
1  (5, 1, 7)
2        (1)

BTW:顺便提一句:

DataFrame has special method to work with strings .str which may work also with list / tuples and some of them even with dictionary DataFrame具有处理字符串.str特殊方法,它也可以用于list / tuples ,其中一些甚至可以用于dictionary

It can't get df['col1'].str.keys() because string doesn't have keys but if you use df['col1'].str[10] then you get from all dictionares elements which have key 10它无法获得df['col1'].str.keys()因为string没有keys但是如果您使用df['col1'].str[10]那么您将从所有具有键10字典元素中获取

0    24.0
1     NaN
2     NaN

df["col1"] is not a dictionary - it is a tuple. df["col1"]不是字典 - 它是一个元组。 That explains why you get an AttributeError .这解释了为什么您会收到AttributeError You need to iterate over each row in the dataframe column and call keys() on it.您需要遍历数据帧列中的每一行并对其调用keys()

df['col1'] = [row.keys() for row in df["col1"]]

DataFrame.apply according to the documentation : DataFrame.apply根据文档

Apply a function along an axis of the DataFrame.沿 DataFrame 的轴应用函数。

Luckily, the default axis is columns, not rows.幸运的是,默认轴是列,而不是行。 You are going for a single column, so make your applied function check the current column.您要查找单列,因此请让您的应用函数检查当前列。

df.apply(lambda c: c.keys() if c.name == "col1" else c)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:对每个字典值执行操作 - Python: Perform an operation on each dictionary value 如何遍历具有固定列的熊猫数据框的每一行并根据python中的条件执行操作? - How can I iterate over each row of a pandas data frame with a fixed column and perform operation on the basis of conditions in python? 遍历列的每一行并执行操作 - Iterate through each rows of a column and perform operation 如何对 pandas Dataframe 执行 groupby 操作,其中取列表列的平均值? - How to perform a groupby operation on a pandas Dataframe where the average over a list column is taken? 如何使用索引 label 将 DataFrame 分组并执行操作以根据每个索引在特定列中找到 3 个最大的 - How to divide a DataFrame into groups using index label and perform operation to find 3 largest in a particular column according to each index 如何对另一个 dataframe 的每一列执行逐行操作? - How to perform a row-wise operation against each column of another dataframe? 从字典执行项目操作 - Perform an item operation from a dictionary 有没有办法使用向量化操作在 dataframe 列的每一行上存储字典? - Is there a way to store a dictionary on each row of a dataframe column using a vectorized operation? 如何对熊猫数据框中的列进行迭代和执行操作 - How to iterate & perform operation over columns in pandas dataframe 如何遍历 xpath 并检查图像是否存在并基于该执行操作 - How to iterate over xpath and check if image exists and perform operation based on that
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM