从 3 个不同的 DataFrame 添加匹配值,而不是整个列 Python

发布于 2025-01-14 13:14:36 字数 990 浏览 1 评论 0原文

我有三个不同的 DateFrame(df2019、df2020 和 df2021),并且它们都具有相同的列(这里有一些),并且有一些重叠的“BrandID”:

    BrandID  StockedOutDays  Profit    SalesQuantity
243 01-02760     120        516452.76     64476
138 01-01737     96         603900.0      80520
166 01-02018     125        306796.8      52896
141 01-01770     109        297258.6      39372
965 02-35464     128        214039.2      24240
385 01-03857     92         326255.16     30954
242 01-02757     73         393866.4      67908

我想要做的是添加特定列中的值来自 3 个 DataFrame 的 BrandID。在我的具体情况下,我想添加来自 df2019、df2020 和 df2021 的“BrandID”= 01-02757 的“销售数量”值,并获取一条可以运行以查看单个数字的行。

我四处寻找并尝试了很多不同的东西,但我被困住了。请帮忙,谢谢!

编辑***我正在寻找这样的东西,我想,我只是不知道如何将它们总结在一起:

df2021.set_index('BrandID',inplace=True)
df2020.set_index('BrandID',inplace=True)
df2019.set_index('BrandID',inplace=True)

df2021.loc['01-02757']['SalesQuantity']+df2020.loc['01-02757']['SalesQuantity']+
df2019.loc['01-02757']['SalesQuantity']

I have three different DateFrames (df2019, df2020, and df2021) and the all have the same columns(here are a few) with some overlapping 'BrandID':

    BrandID  StockedOutDays  Profit    SalesQuantity
243 01-02760     120        516452.76     64476
138 01-01737     96         603900.0      80520
166 01-02018     125        306796.8      52896
141 01-01770     109        297258.6      39372
965 02-35464     128        214039.2      24240
385 01-03857     92         326255.16     30954
242 01-02757     73         393866.4      67908

What I'm trying to do is add the value from one column for a specific BrandID from each of the 3 DataFrame's. In my specific case, I'd like to add the value of 'Sales Quantity' for 'BrandID' = 01-02757 from df2019, df2020 and df2021 and get a line I can run to see a single number.

I've searched around and tried a bunch of different things, but am stuck. Please help, thank you!

EDIT *** I'm looking for something like this I think, I just don't know how to sum them all together:

df2021.set_index('BrandID',inplace=True)
df2020.set_index('BrandID',inplace=True)
df2019.set_index('BrandID',inplace=True)

df2021.loc['01-02757']['SalesQuantity']+df2020.loc['01-02757']['SalesQuantity']+
df2019.loc['01-02757']['SalesQuantity']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2025-01-21 13:14:37
import pandas as pd
df2019 = pd.DataFrame([{"BrandID":"01-02760", "StockedOutDays":120, "Profit":516452.76, "SalesQuantity":64476},
          {"BrandID":"01-01737", "StockedOutDays":96, "Profit":603900.0, "SalesQuantity":80520}])

df2020 = pd.DataFrame([{"BrandID":"01-02760", "StockedOutDays":123, "Profit":76481.76, "SalesQuantity":2457},
          {"BrandID":"01-01737", "StockedOutDays":27, "Profit":203014.0, "SalesQuantity":15648}])


df2019["year"] = 2019
df2020["year"] = 2020

df = pd.DataFrame.append(df2019, df2020)
df_sum = df.groupby("BrandID").agg("sum").drop("year",axis=1)

print(df)
print(df_sum)
df:
    BrandID  StockedOutDays     Profit  SalesQuantity  year
0  01-02760             120  516452.76          64476  2019
1  01-01737              96  603900.00          80520  2019
0  01-02760             123   76481.76           2457  2020
1  01-01737              27  203014.00          15648  2020
df_sum:
          StockedOutDays     Profit  SalesQuantity
BrandID                                           
01-01737             123  806914.00          96168
01-02760             243  592934.52          66933
import pandas as pd
df2019 = pd.DataFrame([{"BrandID":"01-02760", "StockedOutDays":120, "Profit":516452.76, "SalesQuantity":64476},
          {"BrandID":"01-01737", "StockedOutDays":96, "Profit":603900.0, "SalesQuantity":80520}])

df2020 = pd.DataFrame([{"BrandID":"01-02760", "StockedOutDays":123, "Profit":76481.76, "SalesQuantity":2457},
          {"BrandID":"01-01737", "StockedOutDays":27, "Profit":203014.0, "SalesQuantity":15648}])


df2019["year"] = 2019
df2020["year"] = 2020

df = pd.DataFrame.append(df2019, df2020)
df_sum = df.groupby("BrandID").agg("sum").drop("year",axis=1)

print(df)
print(df_sum)
df:
    BrandID  StockedOutDays     Profit  SalesQuantity  year
0  01-02760             120  516452.76          64476  2019
1  01-01737              96  603900.00          80520  2019
0  01-02760             123   76481.76           2457  2020
1  01-01737              27  203014.00          15648  2020
df_sum:
          StockedOutDays     Profit  SalesQuantity
BrandID                                           
01-01737             123  806914.00          96168
01-02760             243  592934.52          66933
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文