大熊猫更改列值并基于此更改另一个列值

时间:2019-02-27 12:12:18

标签: python-3.x pandas dataframe

我具有以下数据框,并希望将“ NaN”添加到“ DROP”列下的值“ 1”和“ 2”,并将相应的“ MEAN”列值也更改为“ NaN”。

print (df)
   Flow  DROP   MEAN
0     1   0.0  0.025
1     2   NaN    NaN
2     3   NaN    NaN
3     4   0.0  0.020

之后:

protected async override void OnAppearing()
{
    base.OnAppearing();

    Task.Run(async () =>
    {
        await Task.Delay(TimeSpan.FromSeconds(10));
        if (CrossConnectivity.Current.IsConnected)
        {
            //Some code here

            Device.BeginInvokeOnMainThread(() =>
            {
                 // If you need to update an UI element
            });

        }
    });
 }

2 个答案:

答案 0 :(得分:1)

loc的条件下使用isin

df.loc[df['DROP'].isin([1,2]), ['DROP','MEAN']] = np.nan

或通过比较不等于0的值:

df.loc[df['DROP'] != 0, ['DROP','MEAN']] = np.nan

答案 1 :(得分:0)

您也可以使用np.where

df['DROP'], df['MEAN'] = np.where(df.DROP != 0, np.nan, df['DROP']), np.where(df.DROP != 0, np.nan, df['MEAN'])

结果:

   Flow  DROP   MEAN
0     1   0.0  0.025
1     2   NaN    NaN
2     3   NaN    NaN
3     4   0.0  0.020