三、pandas新增数据列

一、读取数据

数据内容如下:

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)
image.png

二、直接赋值新增列

df.loc[:,'bwendu'] = df['bwendu'].str.replace('C','').astype('int32')
df.loc[:,'ywendu'] = df['ywendu'].str.replace('C','').astype('int32')
image.png
df['wencha2'] = df['bwendu'] - df['ywendu']
image.png

三、使用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)
image.png

四、使用assign同时新增多列

df.assign(
    ywendu_huashi = lambda x:x['ywendu'] * 9 + 2,
    bwendu_huashi = lambda x:x['bwendu'] * 9 + 2
)
image.png

五、按条件新增列

df['wencha_type2'] = '温差正常'
df.loc[df['bwendu']-df['ywendu']>=11, 'wencha_type2'] = '温差大'
df.loc[df['bwendu']-df['ywendu']<=9, 'wencha_type2'] = '温差小'
image.png

六、统计列中元素的个数

df['wencha_type2'].value_counts()
image.png

你可能感兴趣的:(三、pandas新增数据列)