1

I created a multi-indexed DataFrame wherein I used groupby with mean.

On checking data type of any particular value

(using ChkVlu = df.loc['Index1'].loc['index2'].loc['requiredcolumn'])

I get data type to be numpy.float64. I require the data type to native python float.

If I use ChkVlu = float(ChkVlu) then only one value at a time gets converted from float64 to float, however on using df = df.astype(float) for entire DataFrame I still get data type to be numpy.float64.

I used df = df.astype(float, copy=False) as well as df = df.astype(float, copy=True) but still getting float64 rather than float.

Please help me in converting float64 type DataFrame to float.

Edit1 : Posting the code here where AnnualData.csv has index 1 and index2 as 1st 2 columns and year-month combination as next set of columns in format of 2001-01, 2001-02, 2001-03 ....... 2016-09, 2016-10, 2016-11 with numerical data in each row. I am converting data in quarters with column names having q1, q2 and so on with mean value of each quarter.

 df = pd.read_csv(‘AnnualData.csv')
 df.set_index(['index1, 'index2'],inplace = True)
 def quarters(col):
  if col.endswith(("01", "02", "03")):
   final_col = col[:4] + "q1"
  elif col.endswith(("04", "05", "06")):
   final_col = col[:4] + "q2"
  elif col.endswith(("07", "08", "09")):
   final_col = col[:4] + "q3"
  else:
   final_col = col[:4] + "q4"
  return final_col  
df = df.groupby(quarters, axis = 1).mean()
ChkVlu = df.loc['index1'].loc['index2'].loc['requiredcolumn']
type(ChkVlu)
7
  • What is the reason for your preference on float over np.float64? Commented Oct 20, 2018 at 7:57
  • Can you display the dataframe code you have? Commented Oct 20, 2018 at 8:02
  • Pranav, try df = df.astype(np.float) Commented Oct 20, 2018 at 8:06
  • Hi pygo, I tried that too, but still data type remains float64. I tried pd.to_numeric(df) also but seems it works only on list or 1D array and not on 2D DataFrame Commented Oct 20, 2018 at 8:09
  • Can you update your post with the DataFrame you have to reproduce it to get an answer? Commented Oct 20, 2018 at 8:21

1 Answer 1

1

You can use to_numeric which will give you a float32

or np.float32(x)

then x.item() will give you python float

Sign up to request clarification or add additional context in comments.

7 Comments

@ charlie6411, what about df1 = np.float(x) rather np.float32(x).
I think its depreciated, or will be soon
Should x be a dataframe or a single value, because the suggested method seems to work on single value while error stating 'can only convert an array of size 1 to a Python scalar' is received if attempted on dataframe
to_numeric will work on series, remember dataframe is just a collection of series
I tried df['requiredcolumn'] = pd.to_numeric(df['requiredcolumn']) for one column to check the same, but still output is numpy.float64
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.