简体   繁体   中英

Writing values of a .csv file into dataframe as they are

I have 2 CSV files with similar data as below:

File 1:

1234    | Hamlet
2345    | Shakespeare
234     | Juliet
hello   | Romeo
hi      | 12334

File 2:

1234    | Achilles
2233    | Odysses
22334   | Hector

But when I load these into dataframes, all the values get loaded as string as below:

File 1:

'1234'    | 'Hamlet'
'2345'    | 'Shakespeare'
'234'     | 'Juliet'
'hello'   | 'Romeo'
'hi'      | '12334'

File 2:

'1234'    | 'Achilles'
'2233'    | 'Odysses'
'22334'   | 'Hector'

Is there a way where I can load int values as int and float as float and string as str?

Note: We don't know what data will be present in the csv files

Can't comment due to low karma, but check this out. It may help you.

df['col1'] = pd.to_numeric(df['col1'], errors='coerce')

I don't know what the name of the column was so I just took it as col1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM