简体   繁体   English

根据值填写 Dataframe 列

[英]Fill Dataframe column based on values

I have a dataframe with several columns, each column has binary values.我有一个 dataframe 有几列,每列都有二进制值。 for each columns I have a numpy array with some values with same length of the column.对于每一列,我都有一个 numpy 数组,其中一些值与列的长度相同。

I need to fill the column with the numpy array but with one condition: fill only if the value in the dataframe column is 0, if it's one don't fill from the numpy array.我需要用 numpy 数组填充该列,但有一个条件:仅当 dataframe 列中的值为 0 时才填充,如果不是从 Z2EA9510C37F7F821E4941FF75FCB2 数组填充。

Using for loops I would get something like this:使用 for 循环我会得到这样的东西:

 for col in df.columns:
  for i in df[col]:
    if df.loc[i,col] == 0:
      df.at[i,col] = arr[i,col]
    else:
      continue

However this is slow and my dataframe is a bit big (10k row * 180 columns), I'm looking if there's any elegant pythonic way to do this但是这很慢,而且我的 dataframe 有点大(10k 行 * 180 列),我正在寻找是否有任何优雅的 pythonic 方式来做到这一点

You can try你可以试试

df=df.mask(df==0,arr)

暂无
暂无

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

相关问题 如何根据条件从数据框列名称填充列值? - How to fill column values from dataframe column names based on condition? 尝试根据其他列值填充数据框列 - Try to fill dataframe column based on other column values 根据熊猫数据框的另一列的值填充空值 - Fill null values based on the values of the other column of a pandas dataframe 根据pandas DataFrame中的另一列填充缺失值 - Fill missing values based on another column in a pandas DataFrame 如何根据 Pandas 数据框中的另一列值填充列中的缺失值? - How to fill missing values in a column based on another column values in a Pandas dataframe? 如何根据匹配值的辅助数据框的条件在主数据框的列中填充 NaN 以使用多个填充值填充 NaN - How to Fill NaNs in Column of Main Dataframe Based On Conditions Matching Secondary Dataframe of Values to Fill NaNs With Multiple Filler Values 根据相互列替换/填充另一个过滤对象(另一个索引)中一个过滤对象(子集)中的值 - Replace/Fill values in a (filtered) subset of a dataframe from another dataframe (differently indexed) based on a mutual column 根据相邻列填充值 - Fill values based on adjacent column 用列中的先前值填充数据框中的NaN值 - Fill NaN values in dataframe with previous values in column 如果基于其他列的条件不满足,则使用值列表填充 Dataframe 列 - Fill a Dataframe column with list of values if condition is not satisfied based on some other column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM