0

I'm trying to create a Pandas DataFrame from some text data which I'm iterating over (it's a dump of lots of json strings from an API).

Some of the data is small numbers, eg 0.00000001. I'm using the Python Decimal type for these elsewhere in another app. I'm not sure how to get Pandas to recognise that these are decimals. Whether I create Decimal objects from them first, or create a DataFrame just from the strings, Pandas creates generic object columns. There are other columns which are different formats.

To take a simple example:

import pandas as pd
df = pd.DataFrame([Decimal('0.01'),])
df.dtypes

Outputs:

0    object
dtype: object

Should I be using the Decimal type? Am I fundamentally misunderstanding something?

Thanks.

Ludo
  • 2,739
  • 2
  • 28
  • 42

1 Answers1

0

Need type for check Decimal:

from decimal import Decimal
import pandas as pd
df = pd.DataFrame([Decimal('0.01'),])
print (df)
      0
0  0.01

print (type(df.loc[0, 0]))
<class 'decimal.Decimal'>

pandas recognise only some dtypes like int, float ..., all others are objects (strings, lists, Decimals).

I think you can check relative answer.

Community
  • 1
  • 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252