如何基于字符的字符串分割只有在相邻列同一行单元格为空

问题描述 投票:1回答:1

我有一个看起来像这样的数据帧。

这里是我的前视图。

              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'])
python python-3.x
1个回答
1
投票

假设你用大熊猫和DF变量是你上面的数据框,你可以尝试以下方法:

df.loc[df['the_date'].isnull(), 'the_date'] = df['string'].apply(lambda x:x.split('_')[-1])
© www.soinside.com 2019 - 2024. All rights reserved.