【问题标题】:How to apply IF, else, else if condition in Pandas DataFrame如何在 Pandas DataFrame 中应用 IF、else、else if 条件
【发布时间】:2019-12-28 09:29:38
【问题描述】:

我的 pandas DataFrame 中有一个包含国家/地区名称的列。我想使用 if-else 条件在列上应用不同的过滤器,并且必须在具有这些条件的 DataFrame 上添加一个新列。

当前数据帧:-

Company Country
BV 	Denmark
BV 	Sweden
DC 	Norway
BV 	Germany
BV 	France
DC 	Croatia
BV 	Italy
DC 	Germany
BV 	Austria
BV 	Spain

我已经尝试过了,但在这个过程中,我必须一次又一次地定义国家/地区。

bookings_d2.loc[(bookings_d2.Country== '丹麦') | (bookings_d2.Country== 'Norway'), 'Country'] = bookings_d2.Country

在 R 中,我目前正在使用这样的 if else 条件,我想在 python 中实现同样的事情。

R 代码示例 1: ifelse(bookings_d2$COUNTRY_NAME %in% c('丹麦','德国','挪威','瑞典','法国','意大利','西班牙','德国','奥地利','荷兰', '克罗地亚','比利时'), as.character(bookings_d2$COUNTRY_NAME),'Others')

R 代码示例 2: ifelse(bookings_d2$country %in% c('德国'), ifelse(bookings_d2$BOOKING_BRAND %in% c('BV'),'Germany_BV','Germany_DC'),bookings_d2$country)

预期的数据帧:-

Company Country
BV 	Denmark
BV 	Sweden
DC 	Norway
BV 	Germany_BV
BV 	France
DC 	Croatia
BV 	Italy
DC 	Germany_DC
BV 	Others
BV 	Others

【问题讨论】:

  • sn-p 已经贴好了,谢谢
  • 请使用 HTML sn-ps(图片旁边的图标)选项粘贴输入和输出。这有助于我们使用pandas.read_clipboard() 将数据作为数据帧读取。我们不能为此目的使用图片,而且我们大多数人都不想浪费时间通过输入数据来创建数据框。
  • 希望它现在对你有用。

标签: python-3.x pandas numpy dataframe if-statement


【解决方案1】:

你可以使用:

例如1:将Series.isinnumpy.whereloc一起使用,但必须使用~反转掩码:

#removed Austria, Spain
L = ['Denmark','Germany','Norway','Sweden','France','Italy',
     'Germany','Netherlands','Croatia','Belgium']

df['Country'] = np.where(df['Country'].isin(L), df['Country'], 'Others')

替代方案:

df.loc[~df['Country'].isin(L), 'Country'] ='Others'

例如2:使用numpy.select或嵌套np.where

m1 = df['Country'] == 'Germany'
m2 = df['Company'] == 'BV'
df['Country'] = np.select([m1 & m2, m1 & ~m2],['Germany_BV','Germany_DC'], df['Country'])

替代方案:

df['Country'] = np.where(~m1, df['Country'],
                np.where(m2, 'Germany_BV','Germany_DC'))
print (df)
  Company     Country
0      BV     Denmark
1      BV      Sweden
2      DC      Norway
3      BV  Germany_BV
4      BV      France
5      DC     Croatia
6      BV       Italy
7      DC  Germany_DC
8      BV      Others
9      BV      Others

【讨论】:

    【解决方案2】:

    不确定您要达到的具体目标,但我猜大概是这样的:

    df=pd.DataFrame({'country':['Sweden','Spain','China','Japan'], 'continent':[None] * 4})
    
      country continent
    0  Sweden      None
    1   Spain      None
    2   China      None
    3   Japan      None
    
    
    df.loc[(df.country=='Sweden') | ( df.country=='Spain'), 'continent'] = "Europe"
    df.loc[(df.country=='China') | ( df.country=='Japan'), 'continent'] = "Asia"
    
      country continent
    0  Sweden    Europe
    1   Spain    Europe
    2   China      Asia
    3   Japan      Asia
    

    您还可以使用 python 列表推导式:

    df.continent=["Europe" if (x=="Sweden" or x=="Denmark") else "Other" for x in df.country]
    

    【讨论】:

    • 看到你刚刚更新了问题,但原理思路应该是一样的。
    • 我想达到同样的效果,但我想知道我们如何在 if 和 else 条件下做到这一点。你的回答解决了我的问题。但我想知道如何使用 if-else 条件语句来做到这一点。
    • 你可以把这两个df.loc语句想成if和elseif,那么你可以把初始的[None]值想成else(你可以随意使用任何其他值,不是' t 必须是 None)。
    • @ArvindPant - 检查欺骗 - np.select
    【解决方案3】:

    你可以得到它:

    country_others=['Poland','Switzerland']
    
    
    df.loc[df['Country']=='Germany','Country']=df.loc[df['Country']=='Germany'].apply(lambda x: x+df['Company'])['Country']
    df.loc[(df['Company']=='DC') &(df['Country'].isin(country_others)),'Country']='Others'
    

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多