我想从子表中插入对应的值到主表中,如果没有对应的值不匹配,它应该在其他列中添加
import pandas as pd
# Sample main data
main_data = pd.DataFrame({
'J-1': ['2001', '2002', '2003', '2004'],
'X': [3225, 4602, 9412, None],
'Y': [6875, 7557, 175, None],
'Z': [0, 97, 0, None],
'A': [163, 0, 3259, None],
'Other': [468, 410, 500, None],
'In': [10731, 12666, 13346, None],
'Total': [18495.34643, 19086.79928, 20187.56618, None]
})
# Sample subset data
subset_data = pd.DataFrame({
'1st': ['X'],
'Winner': [9,751],
'Winner %': ['75%'],
'2nd': ['av'],
'2nd P': ['Z'],
'2nd Score': [2,760],
'2nd %': ['21%'],
'Margin': [6,991],
'Margin%': ['54%'],
'3rd': ['uv'],
'3rd P': ['S'],
'3rd Score': [304],
'3rd %': ['2%'],
'Others Score': [174],
'Others %': ['1%'],
'In': [12,989]
})
我想要的输出main_data应该是这样的,我想实现下面的结果。
J-1 X Y Z A Other In Total
2001 3225 6875 0 163 468 10731 18495.34643
2002 4602 7557 97 0 410 12666 19086.79928
2003 9412 175 0 3259 500 13346 20187.56618
2004 9751 0 2760 0 478 12989 25978
我试过这个密码,
for column_name in subset_data.columns:
if column_name in main_data.columns:
main_data.loc[zp_2017_index, column_name] = subset_data[column_name].iloc[0]
谁能帮我得到想要的结果?
1条答案
按热度按时间8hhllhi21#
假设年份是第一列,并且您希望使用列名或前一个单元格中的名称进行Map:
更新的DataFrame:
fillna(0)
。另外,请注意,“Other”是缺失的,因为不存在具有此名称的显式列。你必须首先清理列名称。*