简体   繁体   English

python pandas数据框乘以匹配索引或行名的列

[英]python pandas dataframe multiply columns matching index or row name

I have two dataframes,我有两个数据框,

df1:

hash  a  b  c
ABC   1  2  3
def   5  3  4
Xyz   3  2 -1

df2:

hash  v
Xyz   3
def   5

I want to make我要实现

df:
hash  a  b  c
ABC   1  2  3 (= as is, because no matching 'ABC' in df2)
def  25 15 20 (= 5*5 3*5 4*5)
Xyz   9  6 -3 (= 3*3 2*3 -1*3)

as like above,和上面一样,

I want to make a dataframe with values of multiplying df1 and df2 according to their index (or first column name) matched.我想根据匹配的索引(或第一列名称)创建一个数据框,其中 df1 和 df2 的值相乘。 As df2 only has one column (v), all df1's columns except for the first one (index) should be affected.由于 df2 只有一列 (v),除第一列 (索引) 之外的所有 df1 列都应受到影响。

Is there any neat Pythonic and Panda's way to achieve it?有没有简洁的 Pythonic 和 Panda 方法来实现它?

df1.set_index(['hash']).mul(df2.set_index(['hash'])) or similar things seem not work.. df1.set_index(['hash']).mul(df2.set_index(['hash']))或类似的东西似乎不起作用..

One Method:一种方法:

# We'll make this for convenience
cols = ['a', 'b', 'c']

# Merge the DataFrames, keeping everything from df
df = df1.merge(df2, 'left').fillna(1)

# We'll make the v column integers again since it's been filled.
df.v = df.v.astype(int)

# Broadcast the multiplication across axis 0
df[cols] = df[cols].mul(df.v, axis=0)

# Drop the no-longer needed column:
df = df.drop('v', axis=1)

print(df)

Output:输出:

  hash   a   b   c
0  ABC   1   2   3
1  def  25  15  20
2  Xyz   9   6  -3

Alternative Method:替代方法:

# Set indices
df1 = df1.set_index('hash')
df2 = df2.set_index('hash')

# Apply multiplication and fill values
df = (df1.mul(df2.v, axis=0)
        .fillna(df1)
        .astype(int)
        .reset_index())

# Output:

  hash   a   b   c
0  ABC   1   2   3
1  Xyz   9   6  -3
2  def  25  15  20

One approach:一种方法:

df1 = df1.set_index("hash")
df2 = df2.set_index("hash")["v"]

res = df1.mul(df2, axis=0).combine_first(df1)
print(res)

Output输出

         a     b     c
hash                  
ABC    1.0   2.0   3.0
Xyz    9.0   6.0  -3.0
def   25.0  15.0  20.0

The function you are looking for is actually multiply .您正在寻找的功能实际上是multiply

Here's how I have done it:以下是我的做法:

>>> df
  hash  a  b
0  ABC  1  2
1  DEF  5  3
2  XYZ  3 -1

>>> df2
  hash  v
0  XYZ  4
1  ABC  8

df = df.merge(df2, on='hash', how='left').fillna(1)
>>> df
  hash  a  b    v
0  ABC  1  2  8.0
1  DEF  5  3  1.0
2  XYZ  3 -1  4.0


df[['a','b']] = df[['a','b']].multiply(df['v'], axis='index')

>>>df
  hash     a     b    v
0  ABC   8.0  16.0  8.0
1  DEF   5.0   3.0  1.0
2  XYZ  12.0  -4.0  4.0


You can actually drop v at the end if you don't need it.如果不需要,实际上可以在最后删除 v 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过匹配Python / Pandas中的列名和行名,将列与行相乘 - Multiply columns with rows by matching column name and row name in Python / Pandas Pandas/Python 将列乘以行 - Pandas/Python multiply columns by row Python Pandas DataFrame:将列名称与行索引匹配” - Python Pandas DataFrame: Matching column names to row index' 根据索引和列之间的匹配,选择性地将 dataframe 的元素乘以一个数字 - Selectively multiply elements of a dataframe for a number on the basis of the matching between index and columns 获取匹配行值的pandas数据帧索引 - Getting the index of pandas dataframe for matching row values 通过将一个行与另一个列相匹配来乘以 Pandas 数据帧 - Multiply pandas dataframes by matching row of one to columns of another 有没有一种方法可以基于相同的列或索引来乘以“ pandas” DataFrame? - Is there a way to multiply 'pandas' DataFrame based on the same columns or index? 熊猫将数据框中的多个多索引列乘以另一列 - Pandas multiply several multi-index columns in a dataframe by another column 使用行值来决定在熊猫数据框中乘以哪些列 - Using row values to decide what columns to multiply in a pandas dataframe 使用索引元组值作为数据帧的行和列名称将Pandas groupby.groups结果转换为数据帧 - Converting Pandas groupby.groups result into dataframe, using index tuple value as row and columns name of dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM