Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在数据帧中应用IF、else、else IF条件_Python 3.x_Pandas_Numpy_Dataframe_If Statement - Fatal编程技术网

Python 3.x 如何在数据帧中应用IF、else、else IF条件

Python 3.x 如何在数据帧中应用IF、else、else IF条件,python-3.x,pandas,numpy,dataframe,if-statement,Python 3.x,Pandas,Numpy,Dataframe,If Statement,我的熊猫数据框中有一列是国家名称。我想使用if-else条件在列上应用不同的过滤器,并且必须使用这些条件在该数据帧上添加一个新列 当前数据帧:- 公司所在国 丹麦 BV瑞典 挪威特区 BV德国 法国 克罗地亚特区 BV意大利 DC德国 奥地利 BV西班牙不确定你到底想实现什么,但我想这是大致如下的: df=pd.DataFrame({'country':['Sweden','Spain','China','Japan'], 'continent':[None] * 4}) country

我的熊猫数据框中有一列是国家名称。我想使用if-else条件在列上应用不同的过滤器,并且必须使用这些条件在该数据帧上添加一个新列

当前数据帧:-

公司所在国
丹麦
BV瑞典
挪威特区
BV德国
法国
克罗地亚特区
BV意大利
DC德国
奥地利

BV西班牙
不确定你到底想实现什么,但我想这是大致如下的:

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]

我不确定你到底想实现什么,但我猜这是一个大致的过程:

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]
您可以执行以下操作:

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'
您可以执行以下操作:

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'
您可以使用:

例如1:与或
loc一起使用,但必须通过
~
反转遮罩:

#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'
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:使用或嵌套
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.loc[~df['Country'].isin(L), 'Country'] ='Others'
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
您可以使用:

例如1:与或
loc一起使用,但必须通过
~
反转遮罩:

#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'
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:使用或嵌套
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.loc[~df['Country'].isin(L), 'Country'] ='Others'
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

我已经粘贴了代码段,谢谢。请使用HTML代码段(图片旁边的图标)选项粘贴输入和输出。这有助于我们使用
pandas.read\u clipboard()
以数据帧的形式读取数据。我们不能将图片用于此目的&我们大多数人都不想浪费时间通过键入数据来创建数据帧。希望现在它对您有用。我已经粘贴了代码段,谢谢。请使用HTML代码段(图片旁边的图标)选项来粘贴输入和输出。这有助于我们使用
pandas.read\u clipboard()
以数据帧的形式读取数据。我们不能将图片用于此目的&我们大多数人都不想浪费时间通过键入数据来创建数据帧。希望现在它对您有效。看到您刚刚更新了问题,但原则思想应该是相同的。我想实现同样的目标,但我想知道如何在if和else条件下实现。你的回答解决了我的问题。但是我想知道如何使用if-else条件语句。你可以把两个df.loc语句想象成if,elseif,然后你可以把初始的[None]值想象成else(你可以自由使用任何其他值,不必是None)。@ArvindPant-检查dupe-
np。选择
看到你刚刚更新了这个问题,但基本思想应该是一样的。我想达到同样的目的,但我想知道我们如何在if和else条件下做到这一点。你的回答解决了我的问题。但是我想知道如何使用if-else条件语句。你可以将两个df.loc语句想象为if,elseif,然后你可以将初始[None]值想象为else(你可以自由使用任何其他值,不必是None)。@ArvindPant-检查dupe-
np。选择