简体   繁体   English

使用熊猫中的另一列替换一列中的值的有效方法

[英]Efficient way to replace values in one column using another column in pandas

How do I replace string values in a dataframe column[1] using list of string values in a different column[2].如何使用不同列 [2] 中的字符串值列表替换数据框列 [1] 中的字符串值。

Data数据

          0                       1            2              3
0  3000 20%  dummy1 3000 dummy2 20%  [3000, 20%]  dummy1 dummy2

I want to replace string value in column 1 ie "dummy1 3000 dummy2 20%" using list in column 2 ie "[3000, 20%]".我想使用第 2 列中的列表(即“[3000, 20%]”)替换第 1 列中的字符串值,即“dummy1 3000 dummy2 20%”。 So 3000 and 20% are replaced with ""(empty string) from the string to form 3rd column(Result) ie "dummy1 dummy2"所以 3000 和 20% 被替换为字符串中的“”(空字符串)以形成第三列(结果)即“dummy1 dummy2”

Code代码

df = pd.DataFrame([['3000 20%', 'dummy1 3000 dummy2 20%']])
df[2] = df[0].str.split(' ')

def replace_string(x):
    repl_string = str(x[1])
    for key in x[2]:
        repl_string = repl_string.replace(key, '')
    return ' '.join(repl_string.split())

df[3] = df.apply(replace_string, axis=1)

I have currently written the above code, which is slow for large dataframe.我目前已经编写了上面的代码,这对于大型数据帧来说很慢。 How do I improve the efficiency of this code or is there any other way to do this?如何提高此代码的效率或有其他方法可以做到这一点?

Use nested list comprehension:使用嵌套列表理解:

df = pd.DataFrame([['3000 20%', 'dummy1 a 3000 dummy2 20%'],
                   ['abc 2%', 'klmn 3000 dummy2 2%']])
print (df)
          0                         1
0  3000 20%  dummy1 a 3000 dummy2 20%
1    abc 2%       klmn 3000 dummy2 2%

df[3] = [' '.join(y for y in j.split() if y not in i.split()) for i, j in zip(df[0], df[1])]
print (df)
          0                         1                 3
0  3000 20%  dummy1 a 3000 dummy2 20%   dummy1 a dummy2
1    abc 2%       klmn 3000 dummy2 2%  klmn 3000 dummy2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过与熊猫中的另一个数据框匹配替换列表列的有效方法 - Efficient way to replace column of lists by matches with another data frame in Pandas 用另一列 Pandas DataFrame 替换一列中的值 - Replace values from one column with another column Pandas DataFrame 用熊猫中另一列的每个值替换一个列的值 - Replace values of one column for each value of another column in pandas Pandas DF - 循环通过 DF 以从另一列中具有共同值的行中找到一列的最小值的有效方法 - Pandas DF - Efficient way to loop through DF to find minimum values of one column from rows with common values in another column 从另一列pandas df分配值的有效方法 - Efficient way to assign values from another column pandas df 使用 Pandas 将特定列值替换为另一个数据框列值 - Replace specific column values with another dataframe column value using Pandas 在 pandas 中使用另一列设置值 - Setting values in one column using another in pandas Python pandas 用模式(同一列 -A)相对于 Pandas 数据帧中的另一列替换一列(A)的 NaN 值 - Python pandas replace NaN values of one column(A) by mode (of same column -A) with respect to another column in pandas dataframe 基于另一个数据框 python pandas 替换列值 - 更好的方法? - Replace column values based on another dataframe python pandas - better way? 将值从一个csv文件匹配到另一个,并使用pandas / python替换整个列 - Matching values from one csv file to another and replace entire column using pandas/python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM