python panda apply_python pandas.apply()函数索引

解决方案

使用df[['Year', 'Month']]应用:df['exp_counts'] = df[['Year', 'Month']].apply(exp_records, axis=1)

结果:

^{pr2}$

原因

当您的Year和Month列为整数时:df.info()

DatetimeIndex: 5 entries, 2016-06-26 19:30:00 to 2016-07-02 16:10:00

Data columns (total 5 columns):

P 5 non-null float64

N 5 non-null float64

ID 5 non-null int64

Year 5 non-null int64

Month 5 non-null int64

dtypes: float64(2), int64(3)

memory usage: 240.0 bytes

按行访问它们,这使它们浮动:df.T.info()

Index: 5 entries, P to Month

Data columns (total 5 columns):

2016-06-26 19:30:00 5 non-null float64

2016-06-26 20:00:00 5 non-null float64

2016-06-26 21:10:00 5 non-null float64

2016-06-26 21:20:00 5 non-null float64

2016-07-02 16:10:00 5 non-null float64

dtypes: float64(5)

memory usage: 240.0+ bytes

由于df.apply(exp_records, axis=1)是按行进行的,所以实际上要将其转换为行。在

这是您在exp_records中得到的row:P 263.600006

N 5.400000

ID 5.000000

Year 2016.000000

Month 6.000000

Name: 2016-06-26T19:30:00.000000000, dtype: float64

创建仅包含Year和Month列的数据帧会导致转换为float,因为这两列都是整数:df[['Year', 'Month']].T.info()

Index: 2 entries, Year to Month

Data columns (total 5 columns):

2016-06-26 19:30:00 2 non-null int64

2016-06-26 20:00:00 2 non-null int64

2016-06-26 21:10:00 2 non-null int64

2016-06-26 21:20:00 2 non-null int64

2016-07-02 16:10:00 2 non-null int64

dtypes: int64(5)

memory usage: 96.0+ bytes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值