简体   繁体   English

pandas 列使用 function 计算,包括字典查找,“系列”对象是可变的,因此它们不能被散列

[英]pandas column calculated using function including dict lookup, 'Series' objects are mutable, thus they cannot be hashed

I am aware there are tons of questions similar to mine, but I could not find the solution to my question in the last 30 Minutes of looking through dozens of threads.我知道有很多与我类似的问题,但在过去 30 分钟内浏览了几十个线程时,我找不到我的问题的解决方案。

I have a dataframe with hundereds of columns and rows, and use most columns within a function to return a value thats supposed to be added to an additional column.我有一个 dataframe 有数百列和行,并使用 function 中的大多数列来返回一个应该添加到附加列的值。

The problem can be broken down to the following.问题可以分解为以下几点。

lookup = {"foo": 1, "bar": 0}

def lookuptable(input_string, input_factor):
    return lookup[input_string] * input_factor

mydata = pd.DataFrame([["foo", 4], ["bar",3]], columns = ["string","faktor"])
mydata["looked up value"] = lookuptable(mydata["string"], mydata["faktor"])

But this returns:但这会返回:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

Is there a way to avoid this problem without, restructuring the function itself?有没有办法避免这个问题,而不重组 function 本身?

Thanks in advance!提前致谢!

Try this:尝试这个:

lookup = {"foo": 1, "bar": 0}

def lookuptable(data):
    return lookup[data["string"]] * data["faktor"]

mydata = pd.DataFrame([["foo", 4], ["bar",3]], columns = ["string","faktor"])
mydata["looked up value"] = mydata.apply(lookuptable, axis=1)

print(mydata)

    string  faktor  looked up value
0   foo        4        4
1   bar        3        0

Besides of using .apply() , you can use list comprehension with .iterrows()除了使用.apply()之外,您还可以将列表推导与.iterrows()一起使用

mydata["looked up value"] = [lookuptable(row[1]["string"], row[1]["faktor"]) for row in mydata.iterrows()]

Your functions accepts 2 parameters, a string and a integer.您的函数接受 2 个参数,一个字符串和一个 integer。 But you provide 2 pandas series to the function instead.但是您改为向 function 提供 2 个 pandas 系列。 You can iterate through the dataframe however and provide the function with the parameters (row-wise) by using .apply() .但是,您可以遍历 dataframe 并使用.apply()

mydata["looked up value"] = mydata\
.apply(lambda row: lookuptable(row["string"], row["faktor"]), axis=1)

You can do this without function -您可以在没有 function 的情况下执行此操作 -

import pandas as pd
lookup = {"foo": 1, "bar": 0}
mydata = pd.DataFrame([["foo", 4], ["bar",3]], columns = ["string","factor"])
mydata["looked up value"] = mydata['string'].map(lookup) * mydata['factor']

暂无
暂无

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

相关问题 获取类型错误:“系列”对象是可变的,因此在使用 function 将列中的 int 转换为季节时,它们不能被散列 - Getting a TypeError: 'Series' objects are mutable, thus they cannot be hashed when using a function to convert a int in a column to a season Pandas loc 错误:“系列”对象是可变的,因此它们不能被散列 - Pandas loc error: 'Series' objects are mutable, thus they cannot be hashed 系列对象是可变的,因此它们不能在 Python pandas 数据帧上散列 - Series objects are mutable, thus they cannot be hashed on Python pandas dataframe Python 和 Pandas:“系列”对象是可变的,因此它们不能被散列 - Python & Pandas: 'Series' objects are mutable, thus they cannot be hashed pandas Python Series对象是可变的,因此它们不能在查询方法中进行散列 - pandas Python Series objects are mutable, thus they cannot be hashed in query method Pandas 返回错误:“系列”对象是可变的,因此它们不能被散列 - Pandas returns error: 'Series' objects are mutable, thus they cannot be hashed 在pandas系列上使用apply方法获取TypeError'Series'对象是可变的,因此不能将它们散列 - Using apply method on pandas series getting TypeError 'Series' objects are mutable, thus they cannot be hashed 类型错误:“系列”对象是可变的,因此它们不能被散列 - TypeError: 'Series' objects are mutable, thus they cannot be hashed Python:“系列”对象是可变的,因此它们不能被散列 - Python : 'Series' objects are mutable, thus they cannot be hashed “系列”对象是可变的,因此它们不能被散列 - 'Series' objects are mutable, thus they cannot be hashed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM