使用pandas dataframe a我想从一列中提取特殊字符文本& ffill
(forward fill),以填充相同的值,直到下一次出现特殊字符,提取后删除特殊字符行。我已经尝试了以下方法,但没有得到我想要的结果。
输入 Dataframe :
import pandas as pd
df = pd.DataFrame({
'col1': ['White color :', 'I am not really sure how to do this', 'I am not really sure how to do this',
'Black color :', 'I am not ready to solve your issue',
'I am not ready to solve your issue','I am not ready to solve your issue'],
})
df['new_col'] = df['col1'].str.extract('^([^:]+)', expand=False)
mask = df.apply(lambda x: x.str.contains(':')).any(axis=1)
df.loc[mask, :] = df.loc[mask, :].ffill(axis=1)
df
所需输出 Dataframe
col1 new_col
0 I am not really sure how to do this White color
1 I am not really sure how to do this White color
2 I am not ready to solve your issue Black color
3 I am not ready to solve your issue Black color
4 I am not ready to solve your issue Black color
3条答案
按热度按时间s2j5cfk01#
遵循您的方法:
输出:
vjhs03f72#
您可以从
col1
中提取颜色,并将其复制到new_col
中,不带冒号,向前填充(这是因为我们只复制col1
值中有:
的位置)。然后您可以简单地删除col1
中有颜色的行:输出:
bxfogqkk3#
这是一种buteforce,但它的工作就像一个魅力。