在read_csv之后在pandas数据框中选择列时出现关键错误 [英] Key error when selecting columns in pandas dataframe after read_csv

查看:132
本文介绍了在read_csv之后在pandas数据框中选择列时出现关键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将CS​​V文件读入pandas数据框并选择一列,但始终出现关键错误.

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

文件读取成功,我可以在iPython笔记本中查看数据框,但是当我想选择除第一个列以外的任何列时,会引发关键错误.

The file reads in successfully and I can view the dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.

我正在使用以下代码:

import pandas as pd

transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
transactions['quarter']

这是我正在处理的文件: https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv ?dl = 0

This is the file I'm working on: https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0

谢谢!

推荐答案

使用sep='\s*,\s*',以便您处理列名中的空格:

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')

或者,您可以确保CSV文件中没有未加引号的空格,并使用命令(未更改)

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

证明:

print(transactions.columns.tolist())

输出:

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

这篇关于在read_csv之后在pandas数据框中选择列时出现关键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆