为了账号安全,请及时绑定邮箱和手机立即绑定

根据其他数据框列值过滤 pandas 数据框

根据其他数据框列值过滤 pandas 数据框

繁花如伊 2023-09-02 16:15:59
df1:Id   Country  Product1    india    cotton2    germany  shoes3    algeria  bagsdf2:id   Country  Product  Qty   Sales1    India    cotton   25    6352    India    cotton   65    3353    India    cotton   96    4554    India    cotton   78    2555    germany  shoes    25    6356    germany  shoes    65    4587    germany  shoes    96    4558    germany  shoes    69    2559    algeria  bags     25    63510   algeria  bags     89    78811   algeria  bags     96    45512   algeria  bags     78    165我需要根据 df1 中的“国家/地区和产品”列过滤 df2 并创建新的数据框。例如,在 df1 中,有 3 个唯一的国家/地区、类别,因此 df 的数量将为 3。输出:df_India_Cotton :id   Country  Product  Qty   Sales1    India    cotton   25    6352    India    cotton   65    3353    India    cotton   96    4554    India    cotton   78    255df_germany_Product:id   Country  Product  Qty   Sales1    germany  shoes    25    6352    germany  shoes    65    4583    germany  shoes    96    4554    germany  shoes    69    255df_algeria_Product:id  Country  Product  Qty   Sales1   algeria  bags     25    6352   algeria  bags     89    7883   algeria  bags     96    4554   algeria  bags     78    165我还可以使用 pandas 中的基本子集过滤掉这些数据框。df[(df.Country=='India') & (df.Products=='cotton')]它可以解决这个问题,我的 df1 中可能有很多国家/地区、产品的独特组合。
查看完整描述

2 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

您可以创建一个字典并在其中保存所有数据帧。检查下面的代码:


d={}

for i in range(len(df1)):

    name=df1.Country.iloc[i]+'_'+df1.Product.iloc[i]

    d[name]=df2[(df2.Country==df1.Country.iloc[i]) & (df2.Product==df1.Product.iloc[i])]

您可以通过其值来调用每个数据帧,如下所示:


d['India_cotton'] 将给出:


id   Country  Product  Qty   Sales

1    India    cotton   25    635

2    India    cotton   65    335

3    India    cotton   96    455

4    India    cotton   78    255


查看完整回答
反对 回复 2023-09-02
?
DIEA

TA贡献1820条经验 获得超2个赞

尝试创建两个 groupby。使用第一个从第二个中选择:


import pandas as pd


selector_df = pd.DataFrame(data=

                           {

                               'Country':'india germany algeria'.split(),

                               'Product':'cotton shoes bags'.split()

                           })


details_df = pd.DataFrame(data=

                         {

                            'Country':'india india india india germany germany germany germany algeria algeria algeria algeria'.split(),

                            'Product':'cotton cotton cotton cotton shoes shoes shoes shoes bags bags bags bags'.split(),

                            'qty':[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

                         })


selectorgroups = selector_df.groupby(by=['Country', 'Product'])

datagroups = details_df.groupby(by=['Country', 'Product'])

for tag, group in selectorgroups:

    print(tag)

    try:

        print(datagroups.get_group(tag))

    except KeyError:

        print('tag does not exist in datagroup')


查看完整回答
反对 回复 2023-09-02
  • 2 回答
  • 0 关注
  • 87 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信