【发布时间】:2021-10-15 04:23:02
【问题描述】:
我有一个数据框,其中包含一些重复的索引值,其中的列包含两个不同实验的值。如果两个索引实例中都存在值,我想优先考虑 Col_A。我正在努力使用以下算法解决此解决方案。
- 合并具有相同索引的行
Pandas merging rows with the same value and same index
- 使用 combine_first 函数创建新列。如果存在则选择 Col_A,否则选择 Col_B
示例数据
data = {'id':['id3', 'id3', 'id6'],
'Col_A':[11,NaN,3],
'Col_B':[NaN,5,NaN]}
## Insert SO Magic Here
##
output = {'id':['id3', 'id6'],
'Col_Score':[11,3]}
如果有比我提出的算法“更好”的解决方案(更短),请告诉我。
【问题讨论】:
标签: python pandas dataframe indexing data-munging