gpt4 book ai didi

python - Pandas:使用来自另一个数据框的重复行更新多个数据框列

转载 作者:太空宇宙 更新时间:2023-11-04 03:16:35 25 4
gpt4 key购买 nike

我有两个数据帧:df1df2如果df2 中的Display Namedf1Display Name 列中,我想将 df1TypeFormatBehaviorDatatype 值分配给df2 的值。

我已经尝试了所有我能想到的merge方法。我认为 loc 是我最大的希望,但我似乎无法获得正确的赋值语法。另外,我正在寻找一个简洁的答案 - 最好是单行的。

像这样:

df2.loc[df2['Display Name'].isin(df1['Display Name']), /
['Type', 'Format', 'Behavior', 'Datatype']] = ???

我的代码:

import pandas as pd
import numpy as np

df1 = pd.DataFrame(
{'Behavior': ['Attribute', 'Attribute', 'Attribute', 'Attribute', 'Attribute',
'Attribute', 'Attribute', 'Metric', 'Metric', 'Metric', 'Metric',
'Metric', 'Metric', 'Metric', 'Metric'],
'Datatype': ['object', 'object', 'object', 'object', 'object', 'object',
'object', 'int64', 'int64', 'int64', 'int64', 'float64',
'float64', 'float64', 'float64'],
'Display Name': ['Campaign', 'Campaign ID', 'Campaign ID', 'Campaign state',
'Campaign state', 'Currency', 'Currency', 'Impressions',
'Impressions', 'Clicks', 'Clicks', 'CTR', 'CTR', 'Avg. CPC',
'Avg. CPC'],
'Format': ['{}', '{}', '{}', '{}', '{}', '{}', '{}', '{:,.0f}', '{:,.0f}',
'{:,.0f}', '{:,.0f}', '{:.2f}%', '{:.2f}%', '${:,.2f}', '${:,.2f}'],
'Type': ['String', 'String', 'String', 'String', 'String', 'String', 'String',
'Integer', 'Integer', 'Integer', 'Integer', 'Percent', 'Percent',
'Currency', 'Currency']},
columns=['Display Name', 'Type', 'Format', 'Behavior', 'Datatype'])

df2 = pd.DataFrame(
{ 'Behavior': [ 'Attribute', 'Metric', 'Metric', 'Metric', 'Attribute',
'Metric', 'Metric', 'Attribute', 'Metric', 'Metric', 'Metric'],
'Datatype': [ 'object', 'float64', 'float64', 'float64', 'object', 'int64',
'int64', 'object', 'float64', 'float64', 'float64'],
'Display Name': [ 'Match type', 'Destination URL', 'Final URL',
'Mobile final URL', 'Labels', 'Impressions', 'Clicks',
'CTR', 'Avg. CPC', 'Cost', 'Avg. position'],
'Format': [ '{}', '{:.2f}', '{:.2f}', '{:.2f}', '{}', '{:,.0f}', '{:,.0f}',
'{}', '{:.2f}', '{:.2f}', '{:.2f}'],
'Type': [ 'String', 'Float', 'Float', 'Float', 'String', 'Integer',
'Integer', 'String', 'Float', 'Float', 'Float']},
columns=['Display Name', 'Type', 'Format', 'Behavior', 'Datatype'])

df2_vals_in_df1 = df2.loc[df2['Display Name'].isin(df1['Display Name']), df2.columns[:]]
df1_vals_in_df2 = df1.loc[df1['Display Name'].isin(df2['Display Name']), df1.columns[:]]

它的样子:

>>> df1
Display Name Type Format Behavior Datatype
0 Campaign String {} Attribute object
1 Campaign ID String {} Attribute object
2 Campaign ID String {} Attribute object
3 Campaign state String {} Attribute object
4 Campaign state String {} Attribute object
5 Currency String {} Attribute object
6 Currency String {} Attribute object
7 Impressions Integer {:,.0f} Metric int64
8 Impressions Integer {:,.0f} Metric int64
9 Clicks Integer {:,.0f} Metric int64
10 Clicks Integer {:,.0f} Metric int64
11 CTR Percent {:.2f}% Metric float64
12 CTR Percent {:.2f}% Metric float64
13 Avg. CPC Currency ${:,.2f} Metric float64
14 Avg. CPC Currency ${:,.2f} Metric float64

>>> df2
Display Name Type Format Behavior Datatype
0 Match type String {} Attribute object
1 Destination URL Float {:.2f} Metric float64
2 Final URL Float {:.2f} Metric float64
3 Mobile final URL Float {:.2f} Metric float64
4 Labels String {} Attribute object
5 Impressions Integer {:,.0f} Metric int64
6 Clicks Integer {:,.0f} Metric int64
7 CTR String {} Attribute object
8 Avg. CPC Float {:.2f} Metric float64
9 Cost Float {:.2f} Metric float64
10 Avg. position Float {:.2f} Metric float64

>>> df2_vals_in_df1
Display Name Type Format Behavior Datatype
5 Impressions Integer {:,.0f} Metric int64
6 Clicks Integer {:,.0f} Metric int64
7 CTR String {} Attribute object
8 Avg. CPC Float {:.2f} Metric float64

>>> df1_vals_in_df2
Display Name Type Format Behavior Datatype
7 Impressions Integer {:,.0f} Metric int64
8 Impressions Integer {:,.0f} Metric int64
9 Clicks Integer {:,.0f} Metric int64
10 Clicks Integer {:,.0f} Metric int64
11 CTR Percent {:.2f}% Metric float64
12 CTR Percent {:.2f}% Metric float64
13 Avg. CPC Currency ${:,.2f} Metric float64
14 Avg. CPC Currency ${:,.2f} Metric float64

请注意 df1_vals_in_df2 Display Name 可能多次使用相同的名称。它们的 TypeFormatBehaviorDatatype 值在两行中始终是相同的值。

df2 的预期输出:

>>> df2
Display Name Type Format Behavior Datatype
0 Match type String {} Attribute object
1 Destination URL Float {:.2f} Metric float64
2 Final URL Float {:.2f} Metric float64
3 Mobile final URL Float {:.2f} Metric float64
4 Labels String {} Attribute object
5 Impressions Integer {:,.0f} Metric int64 <-- same
6 Clicks Integer {:,.0f} Metric int64 <-- same
7 CTR Percent {:.2f}% Metric float64 <-- changed
8 Avg. CPC Currency ${:,.2f} Metric float64 <-- changed
9 Cost Float {:.2f} Metric float64
10 Avg. position Float {:.2f} Metric float64

要点 #1:第 5、6 行相同,因为它们在 df1df2 中相同。

要点 #2:第 7 行,从 String, {}, Attribute, object 更改为 Percent, {:.2f}%, Metric, float64 - df1 中的行值,因为 df2 中的 Display NameDisplay Name 中找到 df1

要点 #3:第 8 行,更改的原因与要点 #2 中所述的相同。

尝试过:

Q1:Python Pandas: Merge or Filter DataFrame by Another. Is there a Better Way?

没有解决这个问题,因为我没有尝试创建新的数据框;我正在尝试替换现有数据框中的值。

Q2:Replace column values based on another dataframe python pandas - better way?

没有解决这个问题,因为该示例包含一个具有正确值的 df,而我的情况是一个具有正确值和不正确值的 df。

抱歉,这是一个很长的问题。我只是想提供足够的上下文。

最佳答案

我认为 combine_first 将是一个优雅的解决方案,按照 JohnE 的说法,前提是您将 Display Name 设置为索引。这让我想到了另一点。我认为只有当“显示名称”恰好对应于每个表中的一组属性时,您的任务才是明确定义的。假设您可以删除重复项、设置索引并像这样使用 .update:

df1 = df1.drop_duplicates()

df1 = df1.set_index('Display Name')
df2 = df2.set_index('Display Name')

df2_c = df2.copy()

df2.update(df1)
df1.update(df2_c)

del df2_c

如果愿意,您可以使用辅助索引重置 df1 的维度。

关于python - Pandas:使用来自另一个数据框的重复行更新多个数据框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com