簡體   English   中英

僅當相鄰列中同一行的單元格為空時,如何根據字符拆分字符串

[英]How to split string based on character only if cell in same row in adjacent column is blank

我有一個看起來像這樣的數據框。

這是我之前的看法。

              string  the_date

-rw-r--r--   12 30067    10224
-rw-r--r--   64 30067    10224
-rw-r--r--   64 30067    10224

我正在尋找一行或幾行代碼,它將“_”字符后的日期拆分為名為“the_date”的字段,但前提是“the_date”為空。 像這樣的事情,我認為:

df_all_files = pd.DataFrame(df_all_files.string.str.rsplit('',1).tolist(), columns = ['string','the_date'])

假設您使用熊貓,並且df變量是您上面的數據框,您可以嘗試以下操作:

df.loc[df['the_date'].isnull(), 'the_date'] = df['string'].apply(lambda x:x.split('_')[-1])

暫無
暫無

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

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