简体   繁体   中英

Replacing null values of a dataframe with a sub-string of another column value

I'm trying to replaced null values of a dataframe with a value of another column value.

Product name  | Brand   | Rate | Qty  |
--------------|---------|------|------|
samsung note  |   Null  |  5   |  1   |
Nokia Lumia   |   Nokia |  2   |  2   |
One Plus 8    |   Null  |  3   |  3   |

How to get this output? I want to fill the missing value on Brand based on value on keyword in Product Name . The result I need is like:

if value in Product Name = Samsung Note then value in Brand= "samsung"
if value in Product Name = One Plus 8 then value in Brand= "One Plus"
Product name  | Brand   | Rate | Qty  |
--------------|---------|------|------|
samsung note  |  Samsung|  5   |  1   |
Nokia Lumia   |   Nokia |  2   |  2   |
One Plus 8    | One Plus|  3   |  3   |

I would try something like this:

df['column_you_want'] = df.apply(lambda row: row['column_you_want'] if row['column_you_want'] else row['column_string'][start_substring:end_substring]) 

Let me know if it works:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM