从 csv 中读取数据 (pandas)

Read data from csv (pandas)

我有两个 table:

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

对于两个 table 我都使用了代码:

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

但是在第 product_reference_id 列的第二个 table 中漏掉了零。 product_reference_id 和 reference_id 列中的值必须相同。这样我就可以加入 tables.

您确定 CSV 本身有前导 0 吗?您可以粘贴与数据框表中的行相对应的每一行的第一行吗?

假设 CSV 本身都有 0,那么您只需将这些列作为字符串读入即可。由于看起来两个 CSV 中的两个 cols 都是 string-y,那么您可以这样读取它们:

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

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

如果您想将某些列读取为其他数据类型,您可以将 dtype 的字典与各个列和类型一起使用。有关信息,请参阅 the pandas docs for read_csv