一、读取数据
数据内容如下:
ymd,bwendu,ywendu,tianqi,fengxiang,fengli,aqi,aqiinfo,aqiLevel
2018-01-01,3C,-6C,多云,东北风,1-2级,59,良,2
2018-01-02,4C,-6C,多云,东北风,3-4级,60,良,2
2018-01-03,5C,-6C,多云,东北风,5-6级,61,良,2
df = pd.read_csv(data_path)
二、直接赋值新增列
df.loc[:,'bwendu'] = df['bwendu'].str.replace('C','').astype('int32')
df.loc[:,'ywendu'] = df['ywendu'].str.replace('C','').astype('int32')
df['wencha2'] = df['bwendu'] - df['ywendu']
三、使用apply传入函数新增列
def get_wendu_type(x):
if x['bwendu'] >=4:
return 'high temp'
if x['ywendu'] <=-6:
return 'low temp'
return 'normal temp'
df['wendu_type'] = df.apply(get_wendu_type, axis=1)
四、使用assign同时新增多列
df.assign(
ywendu_huashi = lambda x:x['ywendu'] * 9 + 2,
bwendu_huashi = lambda x:x['bwendu'] * 9 + 2
)
五、按条件新增列
df['wencha_type2'] = '温差正常'
df.loc[df['bwendu']-df['ywendu']>=11, 'wencha_type2'] = '温差大'
df.loc[df['bwendu']-df['ywendu']<=9, 'wencha_type2'] = '温差小'
六、统计列中元素的个数
df['wencha_type2'].value_counts()