簡體   English   中英

Python:基於另一列和行的條件函數創建新列

[英]Python: Creating New Column Based on Conditional Function of Another Column and Row

我試圖根據來自不同列和行的數據生成一些新列。 例如,采取以下系列:

df = pd.Series(['Fruit[edit]','Apple','Orange','Banana','Vegetable[edit]','Celery','Beans','Kale'])

0        Fruit[edit]
1              Apple
2             Orange
3             Banana
4    Vegetable[edit]
5             Celery
6              Beans
7               Kale

我從一系列開始,其中帶有“[edit]”的元素代表類別,其余的是屬於該類別的項目的名稱。 我想創建兩個新列,一個顯示“類別”(即水果或蔬菜),另一個列標題為“名稱”,顯示屬於該類別的項目。

最終結果應如下所示:

期望的結果

    Category    Name
0   Fruit       Apple
1   Fruit       Orange
2   Fruit       Banana
3   Vegetable   Celery
4   Vegetable   Beans
5   Vegetable   Kale

當我們下去這個系列時,我希望代碼識別一個新的類別(即以'[edit]'結尾的元素,並將其存儲為項目的更新類別,直到達到更新的類別。

采用:

#if necessary convert Series to DataFrame 
df = df.to_frame('Name')
#get rows with edit
mask = df['Name'].str.endswith('[edit]')
#remove edit
df.loc[mask, 'Name'] = df['Name'].str[:-6]
#create Category column
df.insert(0, 'Category', df['Name'].where(mask).ffill())
#remove rows with same values in columns
df = df[~mask].copy()
print (df)
    Category    Name
1      Fruit   Apple
2      Fruit  Orange
3      Fruit  Banana
5  Vegetable  Celery
6  Vegetable   Beans
7  Vegetable    Kale

這可能是丑陋的,但做的工作:

df = pd.DataFrame(df) #since df is a series
df['Name']=df.groupby(df[0].str.contains('edit').cumsum())[0].apply(lambda x: x.shift(-1))
df=df.dropna().rename(columns={0:'Category'})
df.loc[~df.Category.str.contains('edit'),'Category']=np.nan
df.Category=df.Category.ffill()
df.Category=df.Category.str.split("[").str[0]
print(df)

    Category    Name
0      Fruit   Apple
1      Fruit  Orange
2      Fruit  Banana
4  Vegetable  Celery
5  Vegetable   Beans
6  Vegetable    Kale

您可以使用str.extract根據關鍵字的存在提取組,

new_df = df.str.extract('(?P<Category>.*\[edit\])?(?P<Name>.*)')\
.replace('\[edit\]', '', regex = True).ffill()\
.replace('', np.nan).dropna()

    Category    Name
1   Fruit   Apple
2   Fruit   Orange
3   Fruit   Banana
5   Vegetable   Celery
6   Vegetable   Beans
7   Vegetable   Kale

暫無
暫無

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

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