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

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

问题描述

我正在尝试将 CSV 文件读入 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/81iwm4f2hsohsq3/transactions.csv?dl=0

This is the file I'm working on: https://www.dropbox.com/s/81iwm4f2hsohsq3/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天全站免登陆