Just started working in Python and ran into this error.
I have two data frames A and B. I use one of input columns to key on A and then combining both, I want to key on B. I am getting an error in the second print step. I did look at other similar threads but did not find a solution. Can you help me fix it?
import pandas as pd
A = pd.DataFrame([
{"AC1":"V1", "AC2":"190801"},
{"AC1":"V2", "AC2":"200414"},
])
A = A.set_index("AC1")
B = pd.DataFrame([
{"BC1":"V1","BC2":"190801","BC3":"2019-10-01"},
{"BC1":"V1","BC2":"191201","BC3":"2019-12-01"},
{"BC1":"V2","BC2":"200414","BC3":"2020-01-24"}
])
B["BC3"] = pd.to_datetime(B["BC3"])
B = B.set_index(["BC1","BC2"])
input = pd.DataFrame([
{"X":"V1","State":"FL","Z":100},
{"X":"V2","State":"CA","Z":130},
])
for item in input["X"].unique():
p1 = A.loc[item]
print(p1)
p2 = B.loc[[(item,p1)],"BC3"]
print(p2)
TypeError: 'Series' objects are mutable, thus they cannot be hashed
If I'm understanding this correctly, you want to use every unique value of the X column of the 'input' dataframe, look that up in the A dataframe, get the AC2 value out of it, then use the value of X plus the value of AC2 to look up the value in BC3 in the B dataframe. Is that right?
If so, you just need to add a column selector to the line starting with p1 =
, and remove a set of brackets from the line starting with p2 =
This works for me:
for item in input["X"].unique():
p1 = A.loc[item, "AC2"]
print(p1)
p2 = B.loc[(item, p1), "BC3"]
print(p2)
I get the following output from it:
190801
2019-10-01 00:00:00
200414
2020-01-24 00:00:00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.