简体   繁体   English

pandas DataFrame:用另一个值替换多个列中的值

[英]pandas DataFrame: replace values in multiple columns with the value from another

I've got a pandas DataFrame where I want to replace certain values in a selection of columns with the value from another in the same row.我有一个 pandas DataFrame ,我想用同一行中另一个列的值替换选择列中的某些值。

I did the following:我做了以下事情:

df[cols[23:30]] = df[cols[23:30]].apply(lambda x: x.replace(99, df['col1']))
df[cols[30:36]] = df[cols[30:36]].apply(lambda x: x.replace(99, df['col2']))
  • cols is a list with column names. cols是一个带有列名的列表。
  • 99 is considered a missing value which I want to replace with the (already calculated) Mean for the given class (ie, col1 or col2 depending on the selection) 99 被认为是一个缺失值,我想用给定 class 的(已计算的)平均值(即 col1 或 col2 取决于选择)替换

It works, but time it takes to replace all those values seems to take longer than would be necessary.它有效,但替换所有这些值所需的时间似乎比必要的要长。 I figured there must be a quicker (computationally) way of achieving the same.我认为必须有一种更快(计算上)的方法来实现相同的目标。

Any suggestions?有什么建议么?

You can try:你可以试试:

import numpy as np

df[cols[23:30]] = np.where(df[cols[23:30]] == 99, df[['col1'] * (30-23)], df[cols[23:30]])

df[cols[30:36]] = np.where(df[cols[30:36]] == 99, df[['col2'] * (36-30)], df[cols[30:36]])

df[["col1"] * n] will create dataframe with exactly same column repeated n times, so numpy could use it as a mask for n columns you want to iterate through if 99 is encountered, otherwise taking respective value, which is already there. df[["col1"] * n]将创建 dataframe 与完全相同的列重复n次,因此 numpy 可以将它用作n列的掩码,如果遇到99则要迭代通过,否则取相应的值,这已经是那里。

暂无
暂无

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

相关问题 Python pandas:将多个列替换为与另一个数据帧中的多个列匹配的值 - Python pandas: replace values multiple columns matching multiple columns from another dataframe 用来自另一个 pandas dataframe 的相应字符串值替换多列中的虚拟值 - Replace dummy values in multiple columns with corresponding string values from another pandas dataframe 子集根据另一个数据帧的值在多个列上进行pandas数据帧 - Subset pandas dataframe on multiple columns based on values from another dataframe python替换数据框熊猫中所有列的多个值 - python replace multiple values from all columns in dataframe pandas 使用Pandas将数据框中的值替换为另一个数据框 - Replace values in dataframe from another dataframe with Pandas Pandas:将列表值替换为来自另一个数据帧的一串值 - Pandas: Replace list value to a string of values from another dataframe Pandas:使用基于两列的另一个数据帧中的值替换一个数据帧中的值 - Pandas: replace values in one dataframe with values from another dataframe based on two columns 替换 pandas dataframe 中的值 - Replace values in a pandas dataframe from another 用另一个数据框的值替换熊猫数据框的多个值的最快方法 - Fastest way to replace multiple values of a pandas dataframe with values from another dataframe 根据 pandas Dataframe 中的多列替换多列值 - Replace multiple column value based on multiple columns in pandas Dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM