从存储在两个数据框中的两个数据透视表中减去值

时间:2018-09-04 07:55:57

标签: python pandas dataframe

我有两个表:

df1:[1 rows x 23 columns]
                 1C   1E    1F    1H    1K ...    2M    2P    2S  2U   2W
total          1057  334  3609  3762  1393 ...   328  1611  1426  87  118

df2:[1 rows x 137 columns]
                 1CA  1CB  1CC  1CF  1CJ  1CS ...   2UB  2UJ  2WB  2WC  2WF  2WJ
total            11  381  111   20  527    2 ...    47   34   79    2    1   36

我需要减去两个表之间的值。 例如1C-1CF1E-1EF1F-1FF等。

即我只需要减去工作表2中以F结尾的列。

Answer: 1C=1C-1CF=1037

使用Python代码怎么可能?

注意:

某些“ df1”在“ df2”中没有“ F”

df1:
['1C', '1E', '1F', '1H', '1K', '1M', '1N', '1P', '1Q', '1R', '1S', '1U', '1W', '2C', '2E', '2F', '2H', '2K', '2M', '2P', '2S', '2U', '2W']
df2:
['1CA', '1CB', '1CC', '1CF', '1CJ', '1CS', '1CU', '1EA', '1EB', '1EC', '1EF', '1EJ', '1ES', '1FA', '1FB', '1FC', '1FF', '1FJ', '1FS', '1FT', '1FU', '1HA', '1HB', '1HC', '1HF', '1HJ', '1HS', '1HT', '1HU', '1KA', '1KB', '1KC', '1KF', '1KJ', '1KS', '1KU', '1MA', '1MB', '1MC', '1MF', '1MJ', '1MS', '1MU', '1NA', '1NB', '1NC', '1NF', '1NJ', '1PA', '1PB', '1PC', '1PF', '1PJ', '1PS', '1PT', '1PU', '1QA', '1QB', '1QC', '1QF', '1QJ', '1RA', '1RB', '1RC', '1RF', '1RJ', '1SA', '1SB', '1SC', '1SF', '1SJ', '1SS', '1ST', '1SU', '1UA', '1UB', '1UC', '1UF', '1UJ', '1US', '1UU', '1WA', '1WB', '1WC', '1WF', '1WJ', '1WS', '1WU', '2CA', '2CB', '2CC', '2CJ', '2CS', '2EA', '2EB', '2EJ', '2FA', '2FB', '2FC', '2FJ', '2FU', '2HB', '2HC', '2HF', '2HJ', '2HU', '2KA', '2KB', '2KC', '2KF', '2KJ', '2KU', '2MA', '2MB', '2MC', '2MF', '2MJ', '2MS', '2MT', '2PA', '2PB', '2PC', '2PF', '2PJ', '2PU', '2SA', '2SB', '2SC', '2SF', '2SJ', '2UA', '2UB', '2UJ', '2WB', '2WC', '2WF', '2WJ']´

4 个答案:

答案 0 :(得分:2)

sheet1_columns = sheet1.columns.tolist()
sheet2_expected_columns = ['%sF' % (c) for c in sheet1_columns]
common_columns = list(set(sheet2_expected_columns).intersection(set(sheet2.columns.tolist()))
columns_dict = {c:'%sF' % (c) for c in sheet1_columns}
sheet1_with_new_columns_names = sheet1.df.rename(columns=columns_dict)
sheet1_restriction = sheet1_with_new_columns_names[common_columns]
sheets2_restriction = sheets2[common_columns]
result = sheet1_restriction - sheet2_restriction

可以测试吗?

答案 1 :(得分:1)

您可以尝试以下操作:

sheet2 = sheet2.filter(regex=(".*F$"))  # Leave only 'F' columns in sheet2

sheet2.columns = [i[:-1] for i in sheet2.columns]  # Remove 'F' in the end for column-wise substraction

result = sheet1 - sheet2  # Substract values
result[result.isnull()] = sheet1  # Leave sheet1 values if there's no appropriate 'F' column in sheet2 

注意:如果sheet1中没有合适的带有'F'的列,则sheet2的值将保持不变。

我这样创建了您的数据框:

sheet1 = pd.DataFrame({'1C': [1057], '1E': [334], '1F': [3609], '2F': [3609]})
sheet2 = pd.DataFrame({'1CA': [11], '1CB': [381], '1CC': [111], '1CF': [20], '1EF': [10], '1FF': [15]})

答案 2 :(得分:1)

解决方案

步骤1:过滤df2中带有后缀F的列:

cols = df2.columns[df2.columns.isin([col+'F' for col in df1.columns])]
cols
Index(['1AF', '1GF'], dtype='object')

步骤2:cols使用字符串操作并过滤df1数据帧,然后从df2中减去并分配值df1

df1.loc[:,cols.str[:-1]] = df1[cols.str[:-1]].values - df2[cols].values
df1
        1A  1B  1C  1D  1E  1F  1G  1H  1I  1J
total   70  72  90  46  30  56  10  51  95  34

1A的值:82-12 = 70,1G的值:34-24 = 10。

设置:

df1 = pd.DataFrame(np.random.randint(30,100, size=(1,10)), columns=list('ABCDEFGHIJ'))
df1.columns = ['1'+col for col in df1.columns]
df1.index = ['total']
df1

        1A  1B  1C  1D  1E  1F  1G  1H  1I  1J
total   82  72  90  46  30  56  34  51  95  34


df2 = pd.DataFrame(np.random.randint(10,30, size=(1,7)), columns=list('ABFGHIJ'))
df2.index = ['total']
df2.columns = ['1'+col for col in df2.columns]
df2.columns = [col+'D' for col in df2.columns]
df2.rename(columns={'1AD':'1AF','1GD':'1GF'},inplace=True)
df2
        1AF 1BD 1FD 1GF 1HD 1ID 1JD
total   12  29  29  24  10  12  17

答案 3 :(得分:0)

您可以尝试

result_df = df1.join(df2)

for col in df1.columns:
    if ((col + 'F' in df2.columns):
        result_df[col] = result_df[col] - result_df[col + 'F']