基于现有列值的新列

时间:2019-04-20 08:53:11

标签: python pandas if-statement conditional-statements nan

尝试根据现有的“临时”列值创建新值ChillFrozen

Temp包含以下值:

-18.00C
-20.00C
+10.00C
+19.00C
Nan
DRY

如何使用熊猫实现它

df = pd.DataFrame({'Temp': ['-18.00C', '+10.00c', 'NaN', 'DRY']})

如果Temp <0.0C,它将落在Frozen
如果Temp> 0.0C,它将低于Chill
如果Temp是“ Nan”或“ DRY”,它将属于NA

预期结果:

Temp_Category
Frozen
Chill
NA
NA

1 个答案:

答案 0 :(得分:0)

您可以按字典提取第一个值和Series.map,但始终在数字前提取必要的值+-

df = pd.DataFrame({'Temp': ['-18.00C', '+10.00c', 'NaN', 'DRY', '+0c', '20c']})

d = {'-':'Frozen', '+':'Chill'}
df['new1'] = df['Temp'].str[0].map(d)

另一个想法是提取数值,转换为float并使用numpy.sign,但是如果还有0,则输出也是0,所以输出是NaN

pat = r"([-+]?\d*\.\d+|\d+)"
d1 = {1:'Chill', -1:'Frozen', 0:'Chill'}
df['new2'] = np.sign(df['Temp'].str.extract(pat, expand=False).astype(float)).map(d1)

具有2个条件和numpy.select的解决方案:

pat = r"([-+]?\d*\.\d+|\d+)"
s = df['Temp'].str.extract(pat).astype(float)
df['new3'] = np.select([s >= 0, s < 0], ['Chill','Frozen'], default=np.nan)

如果只有温度的最后一个值是非数字的(例如cC),则可以使用to_numeric通过索引删除最后一个字符:

s = pd.to_numeric(df['Temp'].str[:-1], errors='coerce')
df['new4'] = np.select([s >= 0, s < 0], ['Chill','Frozen'], default=np.nan)
print (df)
      Temp    new1    new2    new3    new4
0  -18.00C  Frozen  Frozen  Frozen  Frozen
1  +10.00c   Chill   Chill   Chill   Chill
2      NaN     NaN     NaN     nan     nan
3      DRY     NaN     NaN     nan     nan
4      +0c   Chill   Chill   Chill   Chill
5      20c     NaN   Chill   Chill   Chill