簡體   English   中英

根據條件組織dataframe中的列

[英]Organize columns in dataframe based on condition

我有一個 dataframe 的產品看起來像這樣

category,number of products
Apple pc,3
Lenovo pc,7
HP pc,4
Apple chargher,6
Lenovo charger,9

如果它們包含相同的字符串(例如 pc 或充電器),我想對類別進行分組並將它們發送到另一個 dataframe 像這樣

category,number of products
pc,14
charger,15

我可以使用 pandas 做到這一點嗎?

嘗試這個

 df['Category'] = df["Category"].apply(lambda x: x.split(" ")[1])
 df1 = df.groupby("Category").sum()

Output

 Category   num_of_product
 charger    15
 pc         14
import pandas as pd

data = {'Name':['Apple pc','Lenovo pc','HP pc','Apple charger','Lenovo charger'],
        'Unit':[3,7,4,6,9]}

df = pd.DataFrame(data)

print(df)

在此處輸入圖像描述

New_df=pd.DataFrame(df['Name'].str.split(' ',1).tolist(),columns=['Company','type'])

New_df['Units']=data['Unit']

print(New_df)

在此處輸入圖像描述

x = New_df[New_df['type']=='pc']['Units'].sum()

y = New_df[New_df['type']=='charger']['Units'].sum()

dfx = pd.DataFrame({'category':['pc','charger'],'number of products':[x,y]}) #creating a new dataframe

print(dfx)

在此處輸入圖像描述

您可以在一行代碼中執行此操作

    In [174]: df
    Out[174]:
             category  number of products
    0        Apple pc                   3
    1       Lenovo pc                   7
    2           HP pc                   4
    3  Apple chargher                   6
    4  Lenovo charger                   9
    
    In [175]: df.groupby([df["category"].str.split().str[-1]])["number of products"].sum()
    Out[175]:
    category
    charger      9
    chargher     6
    pc          14
    Name: number of products, dtype: int64
   
    In [177]: pd.DataFrame(df.groupby([df["category"].str.split().str[-1]])["number of products"].sum()).reset_index()
    Out[177]:
   category  number of products
0   charger                   9
1  chargher                   6
2        pc                  14

您可以嘗試:

導入 pandas 作為 pd

data={'category':['Apple pc','Lenovo pc','HP pc','Apple charger','Lenovo charger'],
      'number of products':[3,7,4,6,9]}

df = pd.DataFrame(data)
new = df["category"].str.split(" ", n = 1, expand = True)
df['brand']=new[0]
df['kind']=new[1]
print(df)

東風:

         category  number of products   brand      kind
0        Apple pc                   3   Apple        pc
1       Lenovo pc                   7  Lenovo        pc
2           HP pc                   4      HP        pc
3  Apple chargher                   6   Apple  chargher
4  Lenovo charger                   9  Lenovo   charger

然后做一個groupby:

print(df.groupby('kind')['number of products'].sum().sort_values())

結果:

kind
pc         14
charger    15
Name: number of products, dtype: int64

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM