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