当前位置:   article > 正文

pandas(四)DataFrame新增列、修改列、删除列_pandas dataframe新增feature

pandas dataframe新增feature
一、pandas 新增数据列

直接赋值、apply、assign、分条件赋值

  • 修改列的值方法
    df.loc[:, 'bWendu'] = df['bWendu'].str.replace('℃', '').astype('int32')
    df.loc[:, 'yWendu'] = df['yWendu'].str.replace('℃', '').astype('int32')
    
    	bWendu	yWendu	tianqi	fengxiang	fengli	aqi	aqiInfo	aqiLevel
    ymd								
    2018-01-01	3	-6~多云	东北风	1-2592
    2018-01-02	2	-5~多云	东北风	1-2491
    2018-01-03	2	-5	多云	北风	1-2281
    2018-01-04	0	-8	阴	东北风	1-2281
    2018-01-05	3	-6	多云~晴	西北风	1-2501
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 新增列 之 赋值的方法
    # 计算一天最高气温与最低气温的差  并在后面赋值一列
    df.loc[:, 'wencha'] = df['bWendu'] - df['yWendu']
    
    	bWendu	yWendu	tianqi	fengxiang	fengli	aqi	aqiInfo	aqiLevel	wencha
    ymd									
    2018-01-01	3	-6~多云	东北风	1-2592	9
    2018-01-02	2	-5~多云	东北风	1-2491	7
    2018-01-03	2	-5	多云	    北风	    1-2281	7
    2018-01-04	0	-8	阴	    东北风	1-2281	8
    2018-01-05	3	-6	多云~晴	西北风	1-2501	9
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 新增列 之 df.apply 的方法
    新增 如果温度大于33高温,小于-10低温,否则常温
    def get_wendu_type(x):
        if x['bWendu'] > 33:
            return '高温'
        if x['yWendu'] < -10:
            return '低温'
        return '常温'
    
    # 设置 columns axis==1; 设置 index axis==0
    df.loc[:, 'wendu_type'] = df.apply(get_wendu_type, axis=1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    获取高低常温的天数  value_counts为分类计数
    df['wendu_type'].value_counts()
    
    wendu_type 
    常温    328
    高温     29
    低温      8
    Name: count, dtype: int64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 新增列 之df.assign 的方法: 可以同时增加多个列
    将温度从摄氏度转位华氏度
    df.assign(
        bWendu_huashi = lambda x: x['bWendu'] * 9 / 5 + 32,
        yWendu_huashi = lambda x: x['yWendu'] * 9 / 5 + 32
    )
    
    	bWendu	yWendu	tianqi	fengxiang	fengli	aqi	aqiInfo	aqiLevel	wencha	wendu_type	bWendu_huashi	yWendu_huashi
    ymd												
    2018-01-01	3	-6~多云	东北风	1-2592	9	常温	37.4	21.2
    2018-01-02	2	-5~多云	东北风	1-2491	7	常温	35.6	23.0
    ...	...	...	...	...	...	...	...	...	...	...	...	...
    2018-12-30	-2	-11~多云	东北风	1311	9	低温	28.4	12.2
    2018-12-31	-2	-10	多云	东北风	1562	8	常温	28.4	14.0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 按条件选择我分组分别赋值:安装条件先选择数据,然后对这部分数据赋值新列
    温差大于10温差大,温差小于10度温差正常
    
    df['wencha_type'] = ''
    df.loc[df['bWendu'] - df['yWendu'] > 10, 'wencha_type'] = '温差大'
    df.loc[df['bWendu'] - df['yWendu'] <= 10, 'wencha_type'] = '温差正常'
    
    	bWendu	yWendu	tianqi	fengxiang	fengli	aqi	aqiInfo	aqiLevel	wencha	wendu_type	wencha_type
    ymd											
    2018-01-01	3	-6~多云	东北风	1-2592	9	常温	温差正常
    2018-01-02	2	-5~多云	东北风	1-2491	7	常温	温差正常
    2018-01-03	2	-5	多云	    北风	    1-2281	7	常温	温差正常
    2018-01-04	0	-8	阴	    东北风	1-2281	8	常温	温差正常
    2018-01-05	3	-6	多云~晴	西北风	1-2501	9	常温	温差正常
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    获取温差大与温差正常的各共天数  value_counts为分类计数
    df['wencha_type'].value_counts()
    
    wencha_type
    温差正常    187
    温差大     178
    Name: count, dtype: int64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
二、如何改变Series和DataFrame对象?
  • 增加或重排: 重新索引
    重新索引: .reindex()能够改变或重排Series和DataFrame索引

  • .reindex()

    参数说明
    index, columns新的行列索引
    fill_value重新索引中,用于填充缺失位置的值
    method填充方法,ffill当前值向前填充,bfill当前值向后填充
    limit最大填充量
    copy默认True,生成新的对象,False时,新旧相等不复制
    import pandas as pd
    dt = {
        '城市': ['北京', '上海', '广州', '深圳', '珠海'],
        '环比': [100, 140, 173, 157, 137],
        '同比': [70, 87, 55, 69, 72],
        '定基': [9, 8, 7, 6, 7]
          }
    df = pd.DataFrame(dt, index=['c1', 'c2', 'c3', 'c4', 'c5'])
    print(df)
    
        城市   环比  同比  定基
    c1  北京  100  70   9
    c2  上海  140  87   8
    c3  广州  173  55   7
    c4  深圳  157  69   6
    c5  珠海  137  72   7
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 调整行顺序

    re_df = df.reindex(index=['c2', 'c1', 'c3', 'c4', 'c5'])
    print(re_df)
    
        城市   环比  同比  定基
    c2  上海  140  87   8
    c1  北京  100  70   9
    c3  广州  173  55   7
    c4  深圳  157  69   6
    c5  珠海  137  72   7
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
  • 调整列顺序

    re_df = df.reindex(columns=['环比', '城市', '同比', '定基'])
    
         环比  城市  同比  定基
    c1  100  北京  70   9
    c2  140  上海  87   8
    c3  173  广州  55   7
    c4  157  深圳  69   6
    c5  137  珠海  72   7
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 新增加一列df.columns.insert(位置, '列名')

    new_c = df.columns.insert(6, '新增')
    
    Index(['城市', '环比', '同比', '定基', '新增'], dtype='object')
    
    • 1
    • 2
    • 3

    新增加一列赋值df.reindex(columns=new_c , fill_value=值)

    new_d = df.reindex(columns=new_c, fill_value=200)
    print(new_d)
    
         新增  城市   环比  同比  定基
    c1  200  北京  100  70   9
    c2  200  上海  140  87   8
    c3  200  广州  173  55   7
    c4  200  深圳  157  69   6
    c5  200  珠海  137  72   7
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
三、索引类型的常用方法
方法说明
.append链接另一个Index对象, 产生新的Index对象
.diff(idx)计算差集,产生新的Index对象
.intersection(idx)计算索引交集
.union(idx)计算索引并集
.delete(loc)删除loc位置处的元素
.insert(loc, e)在loc位置添加一个元素e

删除loc位置处的元素df.columns.delete(index)或者df.columns.delete([index1, index2])

# 删除了同比
nc = df.columns.delete(2)

Index(['城市', '环比', '定基'], dtype='object')
  • 1
  • 2
  • 3
  • 4

在loc位置添加一个元素e

# 在2位置添加一个索引
new_c = df.columns.insert(2, '新增')

Index(['城市', '环比', '新增', '同比', '定基'], dtype='object')
  • 1
  • 2
  • 3
  • 4

输出结果

new_d = df.reindex(columns=new_c, fill_value=200)

    城市   环比   新增  同比  定基
c1  北京  100  200  70   9
c2  上海  140  200  87   8
c3  广州  173  200  55   7
c4  深圳  157  200  69   6
c5  珠海  137  200  72   7

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 删除: drop
    .drop()能够删除Series和DataFrame指定行或列索引

    删除行: df.drop(index)  df.drop([index, index2])
    删除列: df.drop(column)  df.drop([column1, column2], axis=1)
    
    • 1
    • 2
    print(df.drop(['c1', 'c2']))
    
        城市   环比  同比  定基
    c3  广州  173  55   7
    c4  深圳  157  69   6
    c5  珠海  137  72   7
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/635081
推荐阅读
相关标签
  

闽ICP备14008679号