簡體   English   中英

將列值的字符替換為Pandas中另一列的字符串

[英]Replace character of column value with string from another column in Pandas

name value_1
dd3   what, _ is
dd4   what, _ is

如何用名稱列中的整個字符串替換value_1列中的'_'?

value_1列的所需輸出

value_1
what, dd3 is
what, dd4 is

我已經嘗試過:

df['value_1'] = df['value_1'].apply(lambda x:x.replace("_", df['name']))

我得到了這個error :expected a string or other character buffer object

apply with axis=1用於按行處理:

df['value_1'] = df.apply(lambda x:x['value_1'].replace("_", x['name']), axis=1)
print (df)
  name       value_1
0  dd3  what, dd3 is
1  dd4  what, dd4 is

更新:類似於@jezrael的解決方案,但對於較大的數據集(矢量化方法)應該更快一些:

In [221]: df['value_1'] = (df.groupby('name')['value_1']
                             .transform(lambda x: x.str.replace('_', x.name)))

In [222]: df
Out[222]:
  name       value_1
0  dd3  what, dd3 is
1  dd4  what, dd4 is

舊答案:

您可以創建一個幫助DF:

In [181]: x = df.value_1.str.split('_', expand=True)

In [192]: x
Out[192]:
        0    1
0  what,    is
1  what,    is

然后在其中插入新列:

In [182]: x.insert(1, 'name', df['name'])

產生:

In [194]: x
Out[194]:
        0 name    1
0  what,   dd3   is
1  what,   dd4   is

並替換原始列:

In [183]: df['value_1'] = x.sum(1)

In [184]: df
Out[184]:
  name       value_1
0  dd3  what, dd3 is
1  dd4  what, dd4 is

暫無
暫無

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

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