我有一个python dict / hashmap:
Generated Code | Contract Date | Contract Time
null | 201607 | 1:31:01
我正在尝试使用相应的地图值替换DataFrame对象中的值。我用下面的预期输出写了一个例子。
我有3列数据。如果生成的代码为null,我将使用Contract Time + map value填充该列。
Generated Code | Contract Date | Contract Time
1:31:016 | 201607 | 1:31:01
预期产出:
yearCode = df['Contract Date'].astype(str).apply(lambda x: x[:4])
df.loc[df["Generated Code"].isnull(),'Generated Code'] = df['Contract Time'] + yearMapping.get(yearCode)
到目前为止我在做什么:
$type = 'testing';
$stmt = $con->prepare("INSERT INTO contents (type, reporter, description) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $type , 'john', 'whatever');
$stmt->execute();
$stmt->close();
我一直收到错误消息: TypeError:' Series'对象是可变的,因此它们不能被散列
这甚至可以吗?
答案 0 :(得分:0)
我认为您需要null
从NaN
到Series
,然后replace
以及最后yearCode
dict
indexing with str { {1}}:
df['Generated Code'] = df['Generated Code'].replace({'null':np.nan})
yearMapping = {"2016":"6", "2017":"7", "2018":"8", "2019":"9"}
yearCode = df['Contract Date'].astype(str).str[:4]
df.loc[df["Generated Code"].isnull(),'Generated Code'] =
df['Contract Time'] + yearCode.map(yearMapping)
print (df)
Generated Code Contract Date Contract Time
0 1:31:016 201607 1:31:01
另一种解决方案是改变条件:
yearMapping = {"2016":"6", "2017":"7", "2018":"8", "2019":"9"}
yearCode = df['Contract Date'].astype(str).str[:4]
df.loc[df["Generated Code"] == 'null','Generated Code'] =
df['Contract Time'] + yearCode.map(yearMapping)
print (df)
Generated Code Contract Date Contract Time
0 1:31:016 201607 1:31:01