pandas 数据帧减法结果具有行和dtype信息 [英] pandas dataframe subtraction result has row and dtype information

查看:141
本文介绍了 pandas 数据帧减法结果具有行和dtype信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的数据框,我计算一个非常简单的减法,如下所示:

  p0_cost = cost_df ['price1'] [cost_df ['date'] == p0]  -  \ 
cost_df ['price2'] [cost_df ['date'] == p0]

除了 p0_cost 之外,我还会计算各种其他统计信息。我将这些统计信息保存到一个 dict()结构中,然后用于创建数据框。



现在,什么发生的是 p0_cost 的值如下:

  12 213.151824 dtype: float64 

实际值 213.151824 但它也保存行和 dtype 信息。



如何获取值而不是所有其他垃圾?

解决方案

您正在创建一个值为系列的dict,如果您只需要使用标量值,则使用 .values [0] 将结果下标:

  p0_cost =(cost_df ['price1'] [cost_df ['date'] == p0]  -  cost_df ['price2'] [cost_df ['date'] == p0] 


I have a simple dataframe and I compute a very simple subtraction like so:

p0_cost =  cost_df['price1'][cost_df['date']==p0] - \
            cost_df['price2'][cost_df['date']==p0]

I compute various other statistics in addition to p0_cost. I save these statistics into a dict() structure which I then use to create a dataframe.

Now, what happens is the value of p0_cost looks like:

12 213.151824 dtype: float64

The actual value 213.151824 but it also saves the row and dtype information.

How do I just get the value and not all this other junk?

解决方案

You're creating a dict with the values of Series, if you want just the scalar value then subscript into the results using .values[0]:

p0_cost =  (cost_df['price1'][cost_df['date']==p0] - cost_df['price2'][cost_df['date']==p0]).values[0]

这篇关于 pandas 数据帧减法结果具有行和dtype信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆