【发布时间】:2021-07-15 06:27:06
【问题描述】:
我有两个数据框。作为示例,请参见下文。 当具有相同的 ProductID 时,如何使用来自 dfB 的相同值填充 df[GrossRate]== 0
基本上我在 df 中的 GrossRate 应该是 150 40 238 32
dataA = {'date': ['20210101','20210102','20210103','20210104'],
'quanitity': [22000,25000,27000,35000],
'NetRate': ['nan','nan','nan','nan'],
'GrossRate': [150,0,238,0],
'ProductID': [9613,7974,1714,5302],
}
df = pd.DataFrame(dataA, columns = ['date', 'quanitity', 'NetRate', 'GrossRate','ProductID' ])
date quanitity NetRate GrossRate ProductID
0 20210101 22000 nan 150 9613
1 20210102 25000 nan 0 7974
2 20210103 27000 nan 238 1714
3 20210104 35000 nan 0 5302
dataB = {
'ProductID': ['9613.T','7974.T','1714.T','5302.T'],
'GrossRate': [10,40,28,32],
}
dfB = pd.DataFrame(dataB, columns = ['ProductID', 'GrossRate' ])
dfB.ProductID = dfB.ProductID.str.replace('.T','')
print (dfB)
ProductID GrossRate
0 9613 10
1 7974 40
2 1714 28
3 5302 32
【问题讨论】:
-
DataFrame 中的行数相同,
roductID中的值顺序相同?
标签: python pandas conditional-statements missing-data