亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據其他數據框列值過濾 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 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號