I have a df:
df:
a b c
date
2012-01-01 0.50 1.2 0.70
2012-01-01 0.45 1.2 0.65
2012-01-01 0.65 1.2 0.63
2012-01-01 0.75 1.2 0.29
2012-01-01 -0.25 1.2 -0.68
I want to calculate: a/b – c
I run:
new = df['a']/df['b'] - df['c']
This returns
new:
date
2012-01-01 -0.2833
2012-01-01 -0.2750
2012-01-01 -0.0883
2012-01-01 0.3350
2012-01-01 0.4717
First error:
If I go:
new.ix[0][0]
TypeError: 'NumericType' object is unsubscriptable
So I assume it’s a series.
So I change it to:
new = pd.DataFrame(new)
type(new.ix[0][0])
<type 'NumericType'>
This is the weird part:
new.ix[0][0]
-0.2833
new.ix[0][0]/2
-0.141650
new.ix[0][0]/2.0
0
What is going on with the number type? How can I change it to float? What is the best practice here?
Thank you.
The reason for the differences in behaviour is when
newis a Series vs. a DataFrame.The first
newyou create is a Series:This has a value in position
0of-0.2833:(You can’t
__getitem__on a float e.g.2.0[0]gives you a similar error.)However, when you then force it to a DataFrame:
this time the 0th index is a Series (row):
Update:
To ensure that all your DataFrames values are floats (
numpy.float64) you canapplymap(which works here since every column is being converted to a float):I don’t know how you got
NumericTypeobject, I assume the divide by 2 vs 2.0 derives from this.