[英]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.