熊猫:在字符串列中填充占位符



我正在使用熊猫数据帧,如下所示:

df = pd.DataFrame(
    [['There are # people', '3', np.nan], ['# out of # people are there', 'Five', 'eight'],
     ['Only # are here', '2', np.nan], ['The rest is at home', np.nan, np.nan]])

导致:

    0                            1     2
0   There are # people           3     NaN
1   # out of # people are there  Five  eight
2   Only # are here              2     NaN
3   The rest is at home          NaN   NaN

我想用第 1 列和第 2 列中的不同字符串替换#占位符,结果是:

0   There are 3 people
1   Five out of eight people are there
2   Only 2 are here
3   The rest is at home

我怎样才能做到这一点?

使用字符串格式

df=df.replace({'#':'%s',np.nan:'NaN'},regex=True)
l=[]
for x , y in df.iterrows():
    if  y[2]=='NaN' and y[1]=='NaN':
        l.append(y[0])
    elif y[2]=='NaN':
        l.append(y[0] % (y[1]))
    else:
        l.append(y[0] % (y[1], y[2]))
l
Out[339]: 
['There are 3 people',
 'Five out of eight people are there',
 'Only 2 are here',
 'The rest is at home']

一种更简洁的方法。

cols = df.columns
df[cols[0]] = df.apply(lambda x: x[cols[0]].replace('#',str(x[cols[1]]),1) if x[cols[1]]!=np.NaN else x,axis=1)
print(df.apply(lambda x: x[cols[0]].replace('#',str(x[cols[2]]),1) if x[cols[2]]!=np.NaN else x,axis=1))

Out[12]:
0                    There are 3 people
1    Five out of eight people are there
2                       Only 2 are here
3                   The rest is at home
Name: 0, dtype: object

如果需要对更多列执行此操作

cols = df.columns
for i in range(1, len(cols)):
    df[cols[0]] = df.apply(lambda x: x[cols[0]].replace('#',str(x[cols[i]]),1) if x[cols[i]]!=np.NaN else x,axis=1)
print(df[cols[0]])

一个通用替换函数,以防您可能有更多值要添加: 使用值列表替换字符串中的给定字符的所有实例(在您的情况下只有两个,但它可以处理更多(

def replace_hastag(text, values, replace_char='#'):
    for v in values:
        if v is np.NaN:
            return text
        else:
            text = text.replace(replace_char, str(v), 1)
    return text

df['text'] = df.apply(lambda r: replace_hastag(r[0], values=[r[1], r[2]]), axis=1)

结果

In [79]: df.text
Out[79]:
0                    There are 3 people
1    Five out of eight people are there
2                       Only 2 are here
3                   The rest is at home
Name: text, dtype: object

最新更新