簡體   English   中英

用另一個列值的子字符串替換 dataframe 的 null 值

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

我正在嘗試用另一個列值的值替換 dataframe 的 null 值。

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

如何獲得這個 output? 我想根據Product Name中的關鍵字值填充Brand上的缺失值。 我需要的結果是:

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   |

我會嘗試這樣的事情:

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]) 

讓我知道它是否有效:)

暫無
暫無

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

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