我有一个数据框dfScore
dfScore = pd.DataFrame([["ringo", 0,0,0]], columns=["Name","Sales total","Problem total","Finance total"])
Name Sales total Problem total Finance total
0 ringo 0 0 0
和数据框类别
data = [["Finance total", 14], ["Sales total", 4], ["Problem total", 5]]
categories = pd.DataFrame(data, columns = ['Category', 'ScoreTruth'])
Category ScoreTruth
0 Finance total 14
1 Sales total 4
2 Problem total 5
我想做的是检查dfScores列中是否包含类别中“类别”中的值。如果是,则将dfScores列中的值设置为“ ScoreTruth”相邻值。我尝试使用isin来获取dfScores列中的索引,但是实际上并没有告诉我哪个Category是哪个索引。即
index = np.where(dfScore.columns.isin(categories["Category"]))
print(index[0])
>>>[1 2 3]
如果我尝试从isin那里获取索引,反过来我会得到
index2 = np.where(categories["Category"].isin(dfScore.columns))
print(index2[0])
>>>[0 1 2]
所以现在我想我可以像这样dfScore.iloc[:,index[0]] = categories.iloc[index2[0]].loc["ScoreTruth"]
来设置值,但是我得到KeyError: 'ScoreTruth'
显然,这只有在我使用index [0]设置dfScores中的每一行的情况下才有效,不理想。
我想输出一个看起来像这样的数据框
Name Sales total Problem total Finance total
0 ringo 4 5 14
答案 0 :(得分:1)
让我们尝试DataFrame.assign
:
s = categories.set_index('Category')['ScoreTruth']
dfScore.assign(**s[s.index.intersection(dfScore.columns)])
Name Sales total Problem total Finance total
0 ringo 4 5 14