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

image-20220109164738541.png

  • 直接赋值

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

      23.png

      实例:计算温差

      24.png

  • 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.否则是常温

      25.png

  • df.assign方法

    • Assign new columns to a DataFrame.

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

      实例:将温度从摄氏度转化为华氏度

      26.png

  • 按条件选择分组分别赋值

    • 按条件先选择数据,然后对这部分数据赋值新列

      实例:高低温差大于10度,则认为温差大

      27.png
      Pandas学习系列说明:本系列仅仅作为Pandas视频的归纳总结与记录,在此感谢蚂蚁学Python


本文由 fmujie 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论

召唤看板娘