pandas 如何从另一个嵌套框架向现有的嵌套框架追加特定的值?

7hiiyaii  于 2023-10-14  发布在  其他
关注(0)|答案(1)|浏览(122)

我想从子表中插入对应的值到主表中,如果没有对应的值不匹配,它应该在其他列中添加

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]

谁能帮我得到想要的结果?

8hhllhi2

8hhllhi21#

假设年份是第一列,并且您希望使用列名或前一个单元格中的名称进行Map:

s = subset_data.loc[0]

mapper = (dict(zip(s, s.iloc[1:])) # map previous value
         |s.to_dict()              # map column name
         )

main_data.iloc[-1, 1:] = main_data.columns[1:].map(mapper)

# add total
main_data.iloc[-1, -1] = main_data.iloc[-1, 1:-1].sum()

更新的DataFrame:

J-1       X       Y       Z       A  Other       In        Total
0  2001  3225.0  6875.0     0.0   163.0  468.0  10731.0  18495.34643
1  2002  4602.0  7557.0    97.0     0.0  410.0  12666.0  19086.79928
2  2003  9412.0   175.0     0.0  3259.0  500.0  13346.0  20187.56618
3  2004  9751.0     NaN  2760.0     NaN    NaN  12989.0  25500.00000
  • 如果需要,您可以进一步fillna(0)。另外,请注意,“Other”是缺失的,因为不存在具有此名称的显式列。你必须首先清理列名称。*

相关问题