如何减去两个不同数据帧之间的行并替换原始值?
How to subtract rows between two different dataframes and replace original value?
我有两个数据框,如下所示。如何通过 10 减 3,55 减 2 来替换 Bank1 数据?
import pandas as pd
data = [['Bank1', 10, 55], ['Bank2', 15,65], ['Bank3', 14,54]]
df1 = pd.DataFrame(data, columns = ['BankName', 'Value1','Value2'])
df2 = pd.DataFrame([[3, 2]], columns = ['Value1','Value2'])
期望输出(仅替换 Bank1 中的值):
BankName
Value1
Value2
Bank1
7
53
Bank2
15
65
Bank3
14
54
尝试,使用 sub
+ combine_first
df1.sub(df2).combine_first(df1)
BankName Value1 Value2
0 Bank1 7.0 53.0
1 Bank2 15.0 65.0
2 Bank3 14.0 54.0
第一个解决方案是通过 Bankname
在 df22
中创建 index
以通过 df1
对齐以进行正确的行减法:
df.set_index('BankName').sub(df2.set_index([['Bank1']]), fill_value=0)
df.set_index('BankName').sub(df2.set_index([['Bank2']]), fill_value=0)
您需要使用 BankName
为 df2
创建新列,在两个 DataFrame
中将 BankName
转换为 index
,因此可以减去这一行:
df22 = df2.assign(BankName = 'Bank1').set_index('BankName')
df = df1.set_index('BankName').sub(df22, fill_value=0).reset_index()
print (df)
BankName Value1 Value2
0 Bank1 7.0 53.0
1 Bank2 15.0 65.0
2 Bank3 14.0 54.0
减去 Bank2
:
df22 = df2.assign(BankName = 'Bank2').set_index('BankName')
df = df1.set_index('BankName').sub(df22, fill_value=0).reset_index()
print (df)
BankName Value1 Value2
0 Bank1 10.0 55.0
1 Bank2 12.0 63.0
2 Bank3 14.0 54.0
另一种过滤方式 BankName
:
m = df1['BankName']=='Bank1'
df1.loc[m, df2.columns] = df1.loc[m, df2.columns].sub(df2.iloc[0])
print (df1)
BankName Value1 Value2
0 Bank1 7 53
1 Bank2 15 65
2 Bank3 14 54
m = df1['BankName']=='Bank2'
df1.loc[m, df2.columns] = df1.loc[m, df2.columns].sub(df2.iloc[0])
我有两个数据框,如下所示。如何通过 10 减 3,55 减 2 来替换 Bank1 数据?
import pandas as pd
data = [['Bank1', 10, 55], ['Bank2', 15,65], ['Bank3', 14,54]]
df1 = pd.DataFrame(data, columns = ['BankName', 'Value1','Value2'])
df2 = pd.DataFrame([[3, 2]], columns = ['Value1','Value2'])
期望输出(仅替换 Bank1 中的值):
BankName | Value1 | Value2 |
---|---|---|
Bank1 | 7 | 53 |
Bank2 | 15 | 65 |
Bank3 | 14 | 54 |
尝试,使用 sub
+ combine_first
df1.sub(df2).combine_first(df1)
BankName Value1 Value2
0 Bank1 7.0 53.0
1 Bank2 15.0 65.0
2 Bank3 14.0 54.0
第一个解决方案是通过 Bankname
在 df22
中创建 index
以通过 df1
对齐以进行正确的行减法:
df.set_index('BankName').sub(df2.set_index([['Bank1']]), fill_value=0)
df.set_index('BankName').sub(df2.set_index([['Bank2']]), fill_value=0)
您需要使用 BankName
为 df2
创建新列,在两个 DataFrame
中将 BankName
转换为 index
,因此可以减去这一行:
df22 = df2.assign(BankName = 'Bank1').set_index('BankName')
df = df1.set_index('BankName').sub(df22, fill_value=0).reset_index()
print (df)
BankName Value1 Value2
0 Bank1 7.0 53.0
1 Bank2 15.0 65.0
2 Bank3 14.0 54.0
减去 Bank2
:
df22 = df2.assign(BankName = 'Bank2').set_index('BankName')
df = df1.set_index('BankName').sub(df22, fill_value=0).reset_index()
print (df)
BankName Value1 Value2
0 Bank1 10.0 55.0
1 Bank2 12.0 63.0
2 Bank3 14.0 54.0
另一种过滤方式 BankName
:
m = df1['BankName']=='Bank1'
df1.loc[m, df2.columns] = df1.loc[m, df2.columns].sub(df2.iloc[0])
print (df1)
BankName Value1 Value2
0 Bank1 7 53
1 Bank2 15 65
2 Bank3 14 54
m = df1['BankName']=='Bank2'
df1.loc[m, df2.columns] = df1.loc[m, df2.columns].sub(df2.iloc[0])