简体   繁体   English

Python pandas - 根据 dataframe1 中的另一列将 dataframe1 中的列与 dataframe2 中的列分开

[英]Python pandas - Divide a column in dataframe1 with a column in dataframe2 based on another column in dataframe1

Dateframe1日期框1

df = pd.DataFrame(SQL_Query, columns=[ X,Y . . . . Currency,Amount] 

Index         X             Y  ...         Currency          Amount
0             74            1  ...         USD               100
1             75            1  ...         EUR               5000
2             76            1  ...         AUD               300
3             79            1  ...         EUR               750

[1411137 rows x 162 columns]

A large SQL query so I avoid writing out all columns.大型 SQL 查询,因此我避免写出所有列。

df1=pd.read_excel(r`FX_EUR.xlsx)

Index       Currency      FX
0             AUD      1.61350
1             BGN      1.95580
2             BRL      4.51450
3             CAD      1.45830
4             CHF      1.09280

So what would I like to achieve is to make a lookup in DF1 to see which Currency is used then divide the "DF1 Amount" column with "DF2 FX" column and to this for all rows in DF1.因此,我想要实现的是在 DF1 中进行查找以查看使用了哪种货币,然后将“DF1 Amount”列与“DF2 FX”列分开,并以此为 DF1 中的所有行。 Either by making a third DF3 or by creating a new column i DF1 called Amount_EUR.通过创建第三个 DF3 或创建一个名为 Amount_EUR 的新列 i DF1。

Any ideas on how to write this code?关于如何编写此代码的任何想法?

You can use a map to apply the transformation -您可以使用map来应用转换 -

import pandas as pd
df = pd.DataFrame({"Currency": ['USD', 'EUR', 'AUD', 'EUR'], "Amount": [100, 5000, 300, 750]})

df1 = pd.DataFrame({"Currency": ["AUD", "BGN", "BRL", "CAD", "EUR"], "FX": [1.6, 1.9, 4.5, 1.5, 1.1]})
df1 = df1.set_index("Currency")

df['FX'] = df['Currency'].map(df1.FX)
df['FX_Adj_Amt'] = df['Amount'].div(df['FX'])

df
#  Currency  Amount   Fx   FX_Adj_Amt
#0      USD     100  NaN          NaN
#1      EUR    5000  1.1  4545.454545
#2      AUD     300  1.6   187.500000
#3      EUR     750  1.1   681.818182

You could use merge to build a series containing the correct FX (same Currency ) with the same index as df .您可以使用 merge 来构建一个包含与df具有相同索引的正确FX (相同Currency )的系列。 The division is then trivial:那么除法是微不足道的:

fx = df.merge(df1, 'left', on='Currency')['FX']
df.loc[~ fx.isna(),'EUR_Amount'] = df.loc[~ fx.isna()]['Amount']/fx.loc[~ fx.isna()]

With your sample data it gives:使用您的示例数据,它提供:

        X  Y  ... Currency  Amount  EUR_Amount
Index                                         
0      74  1  ...      USD     100         NaN
1      75  1  ...      EUR    5000         NaN
2      76  1  ...      AUD     300  185.931205
3      79  1  ...      EUR     750         NaN

暂无
暂无

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

相关问题 如何根据在 dataframe2 中创建的列在 dataframe1 中创建列,该列是通过在 dataframe1 上使用 groupby() 导出的 - How create a column in dataframe1 based on a column created in dataframe2, which is derived by using groupby() on dataframe1 根据 DataFrame2 中的值在 DataFrame1 中创建列 - Create column in DataFrame1 based on values from DataFrame2 交叉连接/合并dataframe1以基于dataframe1中的列创建组合的dataframe2 - cross join/merge dataframe1 to create dataframe2 of combinations based on column in dataframe1 使用现有的列标题将 Dataframe1 行合并到 Dataframe2 - Python Pandas - Merging Dataframe1 rows into Dataframe2 using existing column headers - Python Pandas 将 dataframe1 列值与另一个数据框列值相除 - Divide the dataframe1 column values with another dataframe column value 使用pandas中dataframe1中的一列的值查找dataframe2中特定列的值 - find the value of a specific column in dataframe2 using the value of one column in the dataframe1 in pandas Pandas,查找dataframe1列的哪些值在dataframe2列,在哪一行 - Pandas, search which values of column of dataframe1 are in column of dataframe2, and in which row 在类似于sql like运算符的dataframe1列中找到dataframe2列,并使用pandas列出dataframe2的结果 - find dataframe2 colum in dataframe1 column similar to the sql like operator and list the result from dataframe2 using pandas 仅当特定列至少包含另一列的一个单词时,才从 Dataframe2 合并 Dataframe1 的 Python/Pandas 中的列 - Merge columns in Python/Pandas of Dataframe1 from Dataframe2 only if specific column contains at least one of the words of the other column 连接 DataFrame,其中 DataFrame1 包含 DataFrame2 的缺失值(特定于列)。 DataFrame1 没有 NaN 值 - Concatenating DataFrames where DataFrame1 contains the missing values of DataFrame2 (Column Specific). DataFrame1 does not have NaN values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM