0

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.

  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). It *looks* as if you read one column as string, the other as integer, but you haven't given us enough context. – Prune May 07 '21 at 23:50
  • I have converted these columns to string values. What i need is to have the same values in column product-reference_id as in the column reference_id from the first table. But i dont know why excel load this column without zeros. – new_datascientist May 07 '21 at 23:57
  • We don't know, either, since you haven't completed your MRE. Please do so; then we can give a definitive repair. – Prune May 08 '21 at 00:02
  • Where is excel loading the data from? Is it an export from the dataframe itself (`df.to_excel`)? Does pandas show the right data types for the columns (`df.dtypes`)? – Chris Launey May 08 '21 at 00:04

1 Answers1

1

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.

Chris Launey
  • 136
  • 6