Search code examples
pythonpandasdictionarydataframeany

Find a value of a dictionary in dataframe column and modify it


I dealing with DataFrames and Dictionaries now, and i have a problem, I have a Dictionary "Fruits"

{BN:'Banana', LM:'Lemon', AP:'Apple' ..... etc}

And a DataFrame- "Stock":

   Fruit             Price
0  Sweet Mango           1
1  Green Apple           2
2  Few blue Banana       0
3  Black Banana          5

I wand to do the next thing: replace all the values from Stock['Fruit'] with the Fruits.values() this way: if the value from Fruits appears in the Stock['Fruit'] row it will be replaced this way:

Few blue Banana ---> Banana

Black Banana ---> Banana

now the DataFrame Stock will look this way:

   Fruit             Price
0  Sweet Mango           1
1  Green Apple           2
2  Banana                0
3  Banana                5

I found different codes to replace or to check if values from the Dicitionary appears in the DataFrame

Stock['Fruit'] = Stock.Fruit.map(Fruits)

if (Fruits.values() in Stock['Fruit'] for item in Stock)

any('Mango' in Stock['Fruit'] for index,item in Stock.iterrows())

But i cant find any thing to update the rows of the DataFrame


Solution

  • Use string methods for condition and extracting required values,

    pat = r'({})'.format('|'.join(d.values()))
    cond = df['Fruit'].str.contains('|'.join(d.values()))
    df.loc[cond, 'Fruit'] = df['Fruit'].str.extract((pat), expand = False)
    
        Fruit       Price
    0   Sweet Mango 1
    1   Apple       2
    2   Banana      0
    3   Banana      5
    

    Edit: As @user3483203 suggested, you can fill the missing values with original once the pattern is extracted.

    df['Fruit'] = df['Fruit'].str.extract(pat).fillna(df.Fruit)