我有一种情况:
postStr = """{
"zoneId":"0",
"id":["a","b","c","d","f","g"],
"currencycode":["USD"],
}"""
postData = json.loads(postStr, object_pairs_hook=OrderedDict)
我有一个数据框:
df = {
'id':['a','b','c','d','f','g','h','i','j','k'],
'B':['c','d','e','d','d','c','s','e','s','q'],
'S':['f','g','h','j','e','j','t','r','p','p']
}
df1 = pd.DataFrame(df)
现在我想要一个数据框架,以便如果id在字典中,则B对应的列变为XX
输出:
df = {
'id':['a','b','c','d','f','g','h','i','j','k'],
'B' :['XX','XX','XX','XX','XX','c','s','e','s','q'],
'S' :['f','g','h','j','e','j','t','r','p','p']
}
df1 = pd.DataFrame(df)
请帮助
答案 0 :(得分:1)
df1.loc[df1['id'].isin(postData['id']), 'id'] = 'XX'
print (df1)
id B S
0 XX c f
1 XX d g
2 XX e h
3 XX d j
4 XX d e
5 XX c j
6 h s t
7 i e r
8 j s p
9 k q p
如果需要更多动态解决方案,请在DataFrame和字典中使用intersection
作为列名,并在循环中设置值:
postStr = """{
"S":["f","h"],
"id":["a","b","c","d","f","g"],
"currencycode":["USD"]
}"""
postData = json.loads(postStr, object_pairs_hook=OrderedDict)
print (postData)
OrderedDict([('S', ['f', 'h']),
('id', ['a', 'b', 'c', 'd', 'f', 'g']),
('currencycode', ['USD'])])
df = {
'id':['a','b','c','d','f','g','h','i','j','k'],
'B':['c','d','e','d','d','c','s','e','s','q'],
'S':['f','g','h','j','e','j','t','r','p','p']
}
df1 = pd.DataFrame(df)
for col in df1.columns.intersection(postData.keys()):
df1.loc[df1[col].isin(postData[col]), col] = 'XX'
print (df1)
id B S
0 XX c XX
1 XX d g
2 XX e XX
3 XX d j
4 XX d e
5 XX c j
6 h s t
7 i e r
8 j s p
9 k q p