将基于行条件设置变量列的值为NaN。

huangapple go评论56阅读模式
英文:

Set variable column values to nan based on row condition

问题

我想要能够根据第一列的值可变地更改一列的值。

假设我有以下数据框:

col_ind   col_1   col_2   col_3
    3       a       b       c
    2       d       e       f
    1       g       h       i

我实际上想要执行以下操作:

df.loc[:, df.columns[-df['col_ind']]:] = np.nan

这将导致:

col_ind   col_1   col_2   col_3
    3      nan     nan     nan
    2       d      nan     nan
    1       g       h      nan
英文:

I want to be able to variably change a column value based on the value of the first column.

Say I have a dataframe as follows:

col_ind   col_1   col_2   col_3
    3       a       b       c
    2       d       e       f
    1       g       h       i  

I effectively want to do

df.loc[:, df.columns[-df['col_ind']:]] = np.nan

Which would result in:

col_ind   col_1   col_2   col_3
    3      nan     nan     nan
    2       d      nan     nan
    1       g       h      nan 

答案1

得分: 5

让我们使用广播来检查可以被掩盖的索引。

c = df.columns[1:]
m = range(len(c), 0, -1) <= df['col_ind'].values[:, None]

df[c] = df[c].mask(m)

结果:

   col_ind col_1 col_2 col_3
0        3   NaN   NaN   NaN
1        2     d   NaN   NaN
2        1     g     h   NaN
英文:

Lets use broadcasting to check the indices which can be masked

c = df.columns[1:]
m = range(len(c), 0, -1) &lt;= df[&#39;col_ind&#39;].values[:, None]

df[c] = df[c].mask(m)

Result

   col_ind col_1 col_2 col_3
0        3   NaN   NaN   NaN
1        2     d   NaN   NaN
2        1     g     h   NaN

答案2

得分: 1

你可以获取 df["col_ind"]values,对它们进行迭代并将 slice 设置为 np.nan

vals = df["col_ind"].values
for i, v in enumerate(vals):
    df.iloc[i, -v:] = np.nan
英文:

You can get the values of df[&quot;col_ind&quot;], iterate through them and set the slice to np.nan:

vals = df[&quot;col_ind&quot;].values
for i, v in enumerate(vals):
    df.iloc[i, -v:] = np.nan

答案3

得分: 1

你可以使用apply并指定result_type='broadcast'。 (编辑:借用 @marcelo-paco 的代码)

def make_nan(row):
    row[-row[0]:] = np.nan
    return row

df = pd.DataFrame({'col_ind': [3, 2, 1], 'col_1': ['a', 'd', 'g'], 'col_2': ['b', 'e', 'h'], 'col_3': ['c', 'f', 'i']})
df[:] = df.apply(make_nan, axis=1, result_type='broadcast')
df

这将得到:

   col_ind col_1 col_2 col_3
0       3   NaN   NaN   NaN
1       2     d   NaN   NaN
2       1     g     h   NaN
英文:

You an use apply with result_type=&#39;broadcast&#39;. (Edit: borrowing @marcelo-paco's code)

def make_nan(row):
    row[-row[0]:] = np.nan
    return row

df = pd.DataFrame({&#39;col_ind&#39;: [3, 2, 1], &#39;col_1&#39;: [&#39;a&#39;, &#39;d&#39;, &#39;g&#39;], &#39;col_2&#39;: [&#39;b&#39;, &#39;e&#39;, &#39;h&#39;], &#39;col_3&#39;: [&#39;c&#39;, &#39;f&#39;, &#39;i&#39;]})
df[:] = df.apply(make_nan, axis=1, result_type=&#39;broadcast&#39;)
df

This will give:

col_ind	col_1	col_2	col_3
	3	NaN	     NaN	NaN
	2	d	     NaN	NaN
	1	g	      h	    NaN

答案4

得分: 1

你可以使用当前列的切片创建新列,然后替换原始列的内容。

for i, cn in enumerate(df.columns, 1):
    df[cn] = [*[np.nan]*i, *df[cn].loc[i:]]

> 将基于行条件设置变量列的值为NaN。

英文:

You could create new columns with slices of the current columns and then replace

for i, cn in enumerate(df.columns,1): 
    df[cn] = [*[np.nan]*i, *df[cn].loc[i:]]

> 将基于行条件设置变量列的值为NaN。

huangapple
  • 本文由 发表于 2023年3月9日 12:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定