How can I know the type of a pandas dataframe cell
If you have a column with different types, e.g.
>>> df = pd.DataFrame(data = {"l": [1,"a", 10.43, [1,3,4]]})
>>> df
l
0 1
1 a
2 10.43
4 [1, 3, 4]
Pandas will just state that this Series
is of dtype object
. However, you can get each entry type by simply applying type
function
>>> df.l.apply(type)
0 <type 'int'>
1 <type 'str'>
2 <type 'float'>
4 <type 'list'>
However, if you have a dataset with very different data types, you probably should reconsider its design..
Related videos on Youtube
John Smith
Updated on March 31, 2022Comments
-
John Smith almost 2 years
I have a dataframe, for example:
1 1.3 2,5 4 5
With the following code, I am trying to know what are the types of the different cells of my pandas dataframe:
for i in range (len(data.columns)) : print (" lenth of columns : " + str(len(data.columns)) ) for j in range (len(data[i])) : data[i][j]=re.sub(r'(\d*)\.(\d*)',r'\1,\2',str(data[i][j])) print(str(data[i][j])) print(" est de type : "type(data[i][j])) if str(data[i][j]).isdigit(): print(str(data[i][j]) + " contain a number " )
The problem is when a cell of the dataframe contain a dot, pandas thinks it is a string. So I used regex, in order to change the dot into a comma.
But after that, the types of all my dataframe cells changed to string. My question is: How can I know if a cell of the dataframe is an int or a float? I already tried
isinstance(x, int)
edit : How can I count the number of int and float, with the output of the df.apply(type) for example , I want to know how many cells of my column are int or float
My second question is, why when I have 2.5 , the dataframe give him the str type ?
0 <class 'int'> 1 <class 'str'> 2 <class 'float'> 3 <class 'float'> 4 <class 'int'> 5 <class 'str'> 6 <class 'str'>
Thanks.
-
roganjosh almost 6 years...
df['col_name'].dtype
? -
jpp almost 6 yearsWelcome to SO. Please provide a minimal reproducible example. Also see: How to make good reproducible pandas examples
-
RCA almost 6 yearsPossible duplicate of Determining Pandas Column DataType
-
roganjosh almost 6 yearsA column won't have mixed dtypes, it will default to some
object
type if it's mixed
-
-
John Smith almost 6 yearsThanks, I want to know why , in my panda dataframe, the number 2.5 , which have a dot , is a <type 'str'> ?
-
rafaelc almost 6 years
2.5
is a float;"2.5"
is a str. Pandas read your file as str by default, you'd have to convert all digits to float/integers manually. Pandas usually infer types correctly. but it is hard to do it with mixed types. -
demongolem almost 4 yearsGood answer, but I believe the OP was asking for an entire dataframe and not just a single column. If I do
df.apply(type)
for instance, it will print out the type of each column and not of each cell. -
rafaelc almost 4 years@demongolem if you want each cell, use
df.applymap(type)