小编the*_*erd的帖子

根据行条件将变量列值设置为 nan

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

假设我有一个数据框,如下所示:

col_ind   col_1   col_2   col_3
    3       a       b       c
    2       d       e       f
    1       g       h       i  
Run Code Online (Sandbox Code Playgroud)

我实际上想做

df.loc[:, df.columns[-df['col_ind']:]] = np.nan
Run Code Online (Sandbox Code Playgroud)

这会导致:

col_ind   col_1   col_2   col_3
    3      nan     nan     nan
    2       d      nan     nan
    1       g       h      nan 
Run Code Online (Sandbox Code Playgroud)

python slice python-3.x pandas

2
推荐指数
1
解决办法
75
查看次数

标签 统计

pandas ×1

python ×1

python-3.x ×1

slice ×1