4

I have a pandas dataFrame created through a mysql call which returns the data as object type.

The data is mostly numeric, with some 'na' values.

How can I cast the type of the dataFrame so the numeric values are appropriately typed (floats) and the 'na' values are represented as numpy NaN values?

3 Answers 3

1

Use the replace method on dataframes:

import numpy as np
df = DataFrame({
'k1': ['na'] * 3 + ['two'] * 4,
'k2': [1, 'na', 2, 'na', 3, 4, 4]})

print df

df = df.replace('na', np.nan)

print df

I think it's helpful to point out that df.replace('na', np.nan) by itself won't work. You must assign it back to the existing dataframe.

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

1 Comment

You can use inplace=True
1

df = df.convert_objects(convert_numeric=True) will work in most cases.

I should note that this copies the data. It would be preferable to get it to a numeric type on the initial read. If you post your code and a small example, someone might be able to help you with that.

6 Comments

This doesn't seem to work e.g. s = pd.Series([1, 'na', 3 ,4]); s.convert_objects(convert_numeric=True)
Hmm it works for a DataFrame. I guess they aren't using the same heuristics to recast? EDIT: I guess the example you gave didn't work. I was working with something like s = pd.DataFrame(['1', 'na', '3', '4']). It works for that.
Doesn't seem to... e.g. df = pd.DataFrame(s) :s created github issue
I think its a bug when there is a non-string element
Was just posting that at the Github issue, but then Github broke.
|
1

This is what Tom suggested and is correct

In [134]: s = pd.Series(['1','2.','na'])

In [135]: s.convert_objects(convert_numeric=True)
Out[135]: 
0     1
1     2
2   NaN
dtype: float64

As Andy points out, this doesn't work directly (I think that's a bug), so convert to all string elements first, then convert

In [136]: s2 = pd.Series(['1','2.','na',5])

In [138]: s2.astype(str).convert_objects(convert_numeric=True)
Out[138]: 
0     1
1     2
2   NaN
3     5
dtype: float64

1 Comment

created issue... I guess the standard claim is that it should already be converted before this point!

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.