我有一个数据帧
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
我想做的是检查类别中“Category”的值是否包含在dfScores的列中。如果是,则将dfScores列中的值设置为“ScoreTruth”相邻值。我曾尝试使用isin获取dfScores列中的索引,但实际上并没有告诉我哪个类别是哪个索引。i、 e
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'
显然,如果我使用索引[0]来设置dfScores中的每一行,这只会起作用,这并不理想
我想输出一个如下所示的数据帧
Name Sales total Problem total Finance total
0 ringo 4 5 14
让我们试试
DataFrame.assign
:相关问题 更多 >
编程相关推荐