【发布时间】:2022-01-20 19:17:55
【问题描述】:
我有一个 pandas 数据框,我需要用嵌套字典中的相应数字替换所有“是”值。嵌套字典由“Store”列的行值作为外部键组成。内部键是名为“A”和“B”的列。
这是数据框:
import pandas as pd
data = [['abc', 'jan','yes','no'], ['abc', 'feb','no','yes'], ['def', 'jan', 'yes','yes'], ['def', 'feb', 'no','yes']]
df = pd.DataFrame(data, columns = ['Store', 'Month', 'A','B' ])
df
Store Month A B
0 abc jan yes no
1 abc feb no yes
2 def jan yes yes
3 def feb no yes
这是嵌套字典:
# dict = {row value in 'Store' column:{column:point value}}
dict = {'abc':{'A':5,'B':4},'def':{'A':3,'B':2}}
这是所需的输出:
Store Month A B
0 abc jan 5 no
1 abc feb no 4
2 def jan 3 2
3 def feb no 2
【问题讨论】:
标签: python pandas dataframe dictionary