Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check each column data type in csv python?

DRIVER’S NAME,LICENSE,PH NUMBER,DOB,HIRE DAY 
CARLOS ARELLANO ,A645-100-63-345-0,(786) 424 1186,24/09/1959,08/01/2008 
ISAEL PENA,P515-400-76 346-0,(305) 915 9316,25/09/1972,27/11/2010 
YUSEL GONZALEZ ,G524-960-78-013-0,(786 ) 616- 1023,12/01/1974,25/08/2012 
JESUS RAMOS ,R523-421-79-409-0,(352) 223-7929,08/11/1975,12/07/2012 
DAVID GOLBOURNE ,G416-164-71-058-1,( 786 ) 251-7144,17/02/1967,06/09/2012 
,,,, 
,,,,

i have csv file like this when i am reading this file python gives "string" as a data-type how i can check each column data type using csv library

like image 683
soheshdoshi Avatar asked May 21 '26 08:05

soheshdoshi


1 Answers

import pandas as pd
out = pd.read_cvs('./path_to_csv')

out['LICENSE'] 

returns

0    A645-100-63-345-0
1    P515-400-76 346-0
2    G524-960-78-013-0
3    R523-421-79-409-0
4    G416-164-71-058-1
Name: LICENSE, dtype: object

as required

If you want a dictionary:

dict(out)

gives you

 {'DRIVER’S NAME': 0    CARLOS ARELLANO 
 1          ISAEL PENA
 2     YUSEL GONZALEZ 
 3        JESUS RAMOS 
 4    DAVID GOLBOURNE 
 Name: DRIVER’S NAME, dtype: object, 'LICENSE': 0    A645-100-63-345-0
 1    P515-400-76 346-0
 2    G524-960-78-013-0
 3    R523-421-79-409-0
 4    G416-164-71-058-1
 Name: LICENSE, dtype: object, 'PH NUMBER': 0      (786) 424 1186
 1      (305) 915 9316
 2    (786 ) 616- 1023
 3      (352) 223-7929
 4    ( 786 ) 251-7144
 Name: PH NUMBER, dtype: object, 'DOB': 0    24/09/1959
 1    25/09/1972
 2    12/01/1974
 3    08/11/1975
 4    17/02/1967
 Name: DOB, dtype: object, 'HIRE DAY ': 0    08/01/2008 
 1    27/11/2010 
 2    25/08/2012 
 3    12/07/2012 
 4    06/09/2012 
 Name: HIRE DAY , dtype: object}

as by your prefered output

like image 103
Benedict K. Avatar answered May 23 '26 10:05

Benedict K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!