pandas 数据帧减法结果具有行和dtype信息 [英] pandas dataframe subtraction result has row and dtype information
问题描述
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屋!