numpy 基于多个条件更新列值

nxagd54h  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(122)

在下面的DataFrame

df = pd.DataFrame([('Ve_Paper', 'Buy', '-','Canada',np.NaN),
                  ('Ve_Gasoline', 'Sell', 'Done','Britain',np.NaN),
                  ('Ve_Water', 'Sell','-','Canada,np.NaN),
                  ('Ve_Plant', 'Buy', 'Good','China',np.NaN),
                  ('Ve_Soda', 'Sell', 'Process','Germany',np.NaN)], columns=['Name', 'Action','Status','Country','Value'])

我正在尝试根据以下条件更新Value列如果ActionSell检查Status是否不是-如果为真,则需要将Country的前两个字符更新为Value列否则,如果Status列为-,则Value列需要更新为Name列,不包含字符Ve_,如果Action不是Sell,则将Value列保留为np.NaN
到目前为止,我尝试过的是

import numpy as np
df['Value'] = np.where(df['Action']== 'Sell',df['Country'].str[:2] if df['Status'].str != '-' else df['Name'].str[3:],df['Value'])

但我得到的输出是<pandas.core.strings.StringMethods object at 0x000001EDB8F662B0>,无论我在哪里尝试提取子字符串,因此输出如下所示

Name     Action    Status      Country       Value
   Ve_Paper        Buy         -       Canada      np.NaN
Ve_Gasoline       Sell      Done      Britain  <pandas.core.strings.StringMethods object at 662B0>
   Ve_Water       Sell         -       Canada  <pandas.core.strings.StringMethods object at 0x000001EDB8F662B0>
   Ve_Plant        Buy      Good        China      np.NaN  
    Ve_Soda       Sell    Process     Germany  <pandas.core.strings.StringMethods object at 0x000001EDB8F662B0>

但我期待的output

Name     Action    Status      Country       Value
   Ve_Paper        Buy         -       Canada      np.NaN # Because Action is not Sell
Ve_Gasoline       Sell      Done      Britain          Br # The first two characters of Country Since Action is sell and Status is not "-"
   Ve_Water       Sell         -       Canada       Water # The Name value without 'Ve_' since Action is Sell and the Status is '-'
   Ve_Plant        Buy      Good        China      np.NaN  
    Ve_Soda       Sell    Process     Germany          Ge

正如在下面的答案中提到的,我尝试的方法对某人有效,但对我无效。有没有比我试过的更好或更好的方法,因为那对我不管用

9gm1akwq

9gm1akwq1#

我看不出这里有什么问题。如果可能的话,请您再详细说明一下好吗?
我在下面分享我的这段代码:

import pandas as pd
import numpy as np
df = pd.DataFrame([('Ve_Paper', 'Buy', '-','Canada',np.NaN),
                  ('Ve_Gasoline', 'Sell', 'Done','Britain',np.NaN),
                  ('Ve_Water', 'Sell','-','Canada',np.NaN),
                  ('Ve_Plant', 'Buy', 'Good','China',np.NaN),
                  ('Ve_Soda', 'Sell', 'Process','Germany',np.NaN)], columns=['Name', 'Action','Status','Country','Value'])

df['Value'] = np.where(df['Action']== 'Sell',df['Country'].str[:2] if df['Status'].str != '-' else df['Name'].str[3:],df['Value'])
print(df)

您也可以尝试使用以下代码:

df['Value'] = np.where(df['Action']== 'Sell', df['Country'].astype(str).str[0:2], df['Value'])
9rygscc1

9rygscc12#

使用np.select是一个很好的选择

conda = df.Action.eq('Sell')
condb = df.Status.eq('-')
df['value'] = np.select([conda&condb, conda&~condb],
                        [df.Name.str.split('_').str[1],df.Country.str[:2]],
                        default = np.nan)
df
Out[343]: 
          Name Action   Status  Country  Value  value
0     Ve_Paper    Buy        -   Canada    NaN    NaN
1  Ve_Gasoline   Sell     Done  Britain    NaN     Br
2     Ve_Water   Sell        -   Canada    NaN  Water
3     Ve_Plant    Buy     Good    China    NaN    NaN
4      Ve_Soda   Sell  Process  Germany    NaN     Ge

相关问题