[英]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.