Pandas怎样新增数据列?
在进行数据分析时,经常需要按照一定条件创建新的数据列,然后进行进一步分析。
- 直接赋值
- df.apply方法
- df.assign方法
- 按条件选择分组分别赋值
import pandas as pd
0. 读取csv数据到DataFrame
1.直接赋值的方法
实例: 清理温度列,变成数字类型
实例:计算温度差
2. df.apply 方法
Apply a function along an axis of the DataFrame.
Objects passed to the function are Series objectives whose index is either the DataFrame’s
index (aixs=0) or the DataFrame’s columns (axis=1).
实例: 添加一列温度类型:
- 如果最高温大于33读就是高温
- 低于-10°C是低温
- 否则是常温
3. df.assign方法
Assign new columns to a DataFrame.
Return a new object with all original columns in addition to new ones.
实例:将温度从摄氏温度变为华氏温度
4.按条件选择分组分别赋值
按条件选择数据,然后对这部分数据赋值新列
实例: 高低温查大于10度,则认为温差大