Key error when selecting columns in pandas dataframe after read_csv

后端 未结 3 2127
一向
一向 2020-12-01 10:49

I\'m trying to read in a CSV file into a pandas dataframe and select a column, but keep getting a key error.

The file reads in successfully and I can view the datafr

3条回答
  •  被撕碎了的回忆
    2020-12-01 11:07

    use sep='\s*,\s*' so that you will take care of spaces in column-names:

    transactions = pd.read_csv('transactions.csv', sep=r'\s*,\s*',
                               header=0, encoding='ascii', engine='python')
    

    alternatively you can make sure that you don't have unquoted spaces in your CSV file and use your command (unchanged)

    prove:

    print(transactions.columns.tolist())
    

    Output:

    ['product_id', 'customer_id', 'store_id', 'promotion_id', 'month_of_year', 'quarter', 'the_year', 'store_sales', 'store_cost', 'unit_sales', 'fact_count']
    

提交回复
热议问题