How to check if columns in the excel file contain date type or numeric type values

Viewed 28

I am a beginner in python. I want to check if the columns of excel files contain date type or numeric type values. How can I achieve that? Can anyone please help?

1 Answers

Your question is vague and can be interpretad in different ways.
( ---- I'll update my answer based on your eventual feedback. ----)

import pandas as pd

df = pd.read_excel('test.xlsx')
print(df)

      id username   birthday  isavailable
0  Id001      foo 1998-01-01            1
1  Id002      bar 2000-03-03            0
2  Id003      baz 2000-07-09            1

You can use pandas.DataFrame.info to get the names and types of all the columns.

print(df.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
 #   Column       Non-Null Count  Dtype         
---  ------       --------------  -----         
 0   id           3 non-null      object        
 1   username     3 non-null      object        
 2   birthday     3 non-null      datetime64[ns]
 3   isavailable  3 non-null      int64         
dtypes: datetime64[ns](1), int64(1), object(2)
memory usage: 224.0+ bytes

You can also build a dictionnary (col_name/col_type) by using pandas.DataFrame.dtypes :

print(df.dtypes.astype(str).to_dict())
{'id': 'object', 'username': 'object', 'birthday': 'datetime64[ns]', 'isavailable': 'int64'}
Related