Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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:基于if语句条件填充新的df列_Python_Python 3.x_Pandas_If Statement - Fatal编程技术网

Python:基于if语句条件填充新的df列

Python:基于if语句条件填充新的df列,python,python-3.x,pandas,if-statement,Python,Python 3.x,Pandas,If Statement,我在尝试新的东西。我想根据一些影响另一列值的条件填充一个新的df列 我有一个包含两列(ID、零售商)的数据框。我想根据ID列中的ID填充Retailer列。我知道如何在SQL中使用CASE语句来实现这一点,但如何在python中实现这一点呢 我已经看过这个例子,但它不是我想要的 我希望看到的输出将是以下内容: ID Retailer 0 112 Webmania 1 5898 DataHub 2 32 Webmania 3 9985 TorrentJunkie 4

我在尝试新的东西。我想根据一些影响另一列值的条件填充一个新的df列

我有一个包含两列(ID、零售商)的数据框。我想根据ID列中的ID填充Retailer列。我知道如何在SQL中使用CASE语句来实现这一点,但如何在python中实现这一点呢

我已经看过这个例子,但它不是我想要的

我希望看到的输出将是以下内容:

     ID Retailer
0   112 Webmania
1  5898 DataHub
2    32 Webmania
3  9985 TorrentJunkie
4    23 Apptronix
5   577 Other
6    17 Other
7   200 Other
8   156 Other

使用和用于测试多个值,如果需要测试字符串(如样本数据中的字符串),请将数字更改为数字,如
112
更改为
'112'

m1 = df['ID'].isin(['112','32'])
m2 =  df['ID'] == '5898'
m3 =  df['ID'] == '9985'
m4 =  df['ID'] == '23'
vals = ['Webmania', 'DataHub', 'TorrentJunkie', 'Apptronix']
masks = [m1, m2, m3, m4]

df['Retailer'] = np.select(masks, vals, default='Other')
print(df)

     ID       Retailer
0   112       Webmania
1  5898        DataHub
2    32       Webmania
3  9985  TorrentJunkie
4    23      Apptronix
5   577          Other
6    17          Other
7   200          Other
8   156          Other
如果还可以使用多个类别,请使用带有自定义功能的解决方案:

def get_data(x):
    if x in ('112','32'):
        return 'Webmania'
    elif x == '5898':
        return 'DataHub'
    elif x == '9985':
        return 'TorrentJunkie'
    elif x == '23':
        return 'Apptronix'
    else: return 'Other'


df['Retailer'] =  df['ID'].apply(get_data)
print (df)
     ID       Retailer
0   112       Webmania
1  5898        DataHub
2    32       Webmania
3  9985  TorrentJunkie
4    23      Apptronix
5   577          Other
6    17          Other
7   200          Other
8   156          Other
或者按字典使用
map
,如果不匹配,则获取
NaN
,因此添加
fillna

d = {'112': 'Webmania','32':'Webmania',
    '5898':'DataHub',
    '9985':'TorrentJunkie',
    '23':'Apptronix'}

df['Retailer'] =  df['ID'].map(d).fillna('Other')

一般来说,请注意“112”(一个文本字符串)与112(一个数字)不同,因此在检查两个值是否相等时,如果混用类型,则不会得到匹配结果。谢谢@MrFelix的提示。我会记住这一点。
d = {'112': 'Webmania','32':'Webmania',
    '5898':'DataHub',
    '9985':'TorrentJunkie',
    '23':'Apptronix'}

df['Retailer'] =  df['ID'].map(d).fillna('Other')