Pandas怎样新增数据列

Pandas怎样新增数据列?

在进行数据分析时,经常需要按照一定条件创建新的数据列,然后进行进一步分析。

  1. 直接赋值
  2. df.apply方法
  3. df.assign方法
  4. 按条件选择分组分别赋值 

0、读取csv数据到dataframe

1、直接赋值的方法

实例:清理温度列,变成数字类型

 实例:计算温差

2、df.apply方法

Apply a function along an axis of the DataFrame.

Objects passed to the function are Series objects whose index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1).

实例:添加一列温度类型:

  1. 如果最高温度大于33度就是高温
  2. 低于-10度是低温
  3. 否则是常温

 

3、df.assign方法

Assign new columns to a DataFrame.

Returns a new object with all original columns in addition to new ones.

实例:将温度从摄氏度变成华氏度

 

4、按条件选择分组分别赋值

按条件先选择数据,然后对这部分数据赋值新列
实例:高低温差大于10度,则认为温差大

posted @ 2021-05-12 18:29  北极星!  阅读(854)  评论(0编辑  收藏  举报