Read data from csv (pandas)

43 views Asked by At

I have two tables:

reference_id exclusiveness
0047465 luxury
0165797 luxury
0013286 selective
BB010 selective
ticket-reference_id product-reference_id
2017010105521000016V 47465
2017010105521000090V 165797
2017010105521000111V 13286
2017010105521000111V BB010

For both tables i have used the code:

    pd.read_csv('df1.csv', sep = ';')
    pd.read_csv('df2.csv', sep = ';')

But in the second table in the column product_reference_id zeros are missed. The values from the column product_reference_id and reference_id have to be the same. So that i could join both tables.

1

There are 1 answers

1
Chris Launey On BEST ANSWER

Are you sure that the CSVs themselves have the leading 0s? Can you paste in the first rows of each that correspond to the rows in your dataframe tables?

Assuming that the CSVs themselves both have the 0s, then you just have to read those columns in as strings. Since it looks like both cols in both CSVs are string-y, then you can read them in like this:

pd.read_csv('df1.csv', dtype=str, sep=';')

pd.read_csv('df2.csv', dtype=str, sep=';')

If you wanted to read some columns in as other datatypes, you can use a dict for dtype with the individual columns and types. See the pandas docs for read_csv for info.