Key error when selecting columns in pandas dataframe after read_csv
up vote
4
down vote
favorite
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 dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.
I am using this code:
import pandas as pd
transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
transactions['quarter']
This is the file I'm working on:
https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0
Thank you!
python csv pandas
add a comment |
up vote
4
down vote
favorite
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 dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.
I am using this code:
import pandas as pd
transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
transactions['quarter']
This is the file I'm working on:
https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0
Thank you!
python csv pandas
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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 dataframe in an iPython notebook, but when I want to select a column any other than the first one, it throws a key error.
I am using this code:
import pandas as pd
transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
transactions['quarter']
This is the file I'm working on:
https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0
Thank you!
python 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.
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.
I am using this code:
import pandas as pd
transactions = pd.read_csv('transactions.csv',low_memory=False, delimiter=',', header=0, encoding='ascii')
transactions['quarter']
This is the file I'm working on:
https://www.dropbox.com/s/imd7hq2iq23hf8o/transactions.csv?dl=0
Thank you!
python csv pandas
python csv pandas
edited Mar 6 '16 at 19:32
MaxU
118k11106163
118k11106163
asked Mar 6 '16 at 19:30
Harry M
1292414
1292414
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
22
down vote
accepted
use sep='s*,s*'
so that you will take care of spaces in column-names:
transactions = pd.read_csv('transactions.csv', sep='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']
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
1
the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now
– Mickey Perlstein
Sep 2 at 11:03
add a comment |
up vote
0
down vote
I finally got the answer how to read a specific column:
import pandas as pd
df=pd.read_csv('titanic.csv',sep='t')
df['Sex']
Because pandas separator uses t
. I hope that works for you.
add a comment |
up vote
0
down vote
The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':
You could also try:
import csv
import pandas as pd
import re
with open (filename, "r") as file:
df = pd.read_csv(file, delimiter = ",")
df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
print(df.columns)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
22
down vote
accepted
use sep='s*,s*'
so that you will take care of spaces in column-names:
transactions = pd.read_csv('transactions.csv', sep='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']
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
1
the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now
– Mickey Perlstein
Sep 2 at 11:03
add a comment |
up vote
22
down vote
accepted
use sep='s*,s*'
so that you will take care of spaces in column-names:
transactions = pd.read_csv('transactions.csv', sep='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']
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
1
the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now
– Mickey Perlstein
Sep 2 at 11:03
add a comment |
up vote
22
down vote
accepted
up vote
22
down vote
accepted
use sep='s*,s*'
so that you will take care of spaces in column-names:
transactions = pd.read_csv('transactions.csv', sep='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']
use sep='s*,s*'
so that you will take care of spaces in column-names:
transactions = pd.read_csv('transactions.csv', sep='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']
edited Feb 7 '17 at 9:00
answered Mar 6 '16 at 19:34
MaxU
118k11106163
118k11106163
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
1
the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now
– Mickey Perlstein
Sep 2 at 11:03
add a comment |
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
1
the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now
– Mickey Perlstein
Sep 2 at 11:03
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
1
1
the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now
– Mickey Perlstein
Sep 2 at 11:03
the list showed me i had an extra space in the name. thanks so much, i have been bashing my head against the wall for a few hrz now
– Mickey Perlstein
Sep 2 at 11:03
add a comment |
up vote
0
down vote
I finally got the answer how to read a specific column:
import pandas as pd
df=pd.read_csv('titanic.csv',sep='t')
df['Sex']
Because pandas separator uses t
. I hope that works for you.
add a comment |
up vote
0
down vote
I finally got the answer how to read a specific column:
import pandas as pd
df=pd.read_csv('titanic.csv',sep='t')
df['Sex']
Because pandas separator uses t
. I hope that works for you.
add a comment |
up vote
0
down vote
up vote
0
down vote
I finally got the answer how to read a specific column:
import pandas as pd
df=pd.read_csv('titanic.csv',sep='t')
df['Sex']
Because pandas separator uses t
. I hope that works for you.
I finally got the answer how to read a specific column:
import pandas as pd
df=pd.read_csv('titanic.csv',sep='t')
df['Sex']
Because pandas separator uses t
. I hope that works for you.
edited Aug 17 at 5:22
answered Aug 15 at 16:46
Bhaskar arya
45
45
add a comment |
add a comment |
up vote
0
down vote
The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':
You could also try:
import csv
import pandas as pd
import re
with open (filename, "r") as file:
df = pd.read_csv(file, delimiter = ",")
df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
print(df.columns)
add a comment |
up vote
0
down vote
The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':
You could also try:
import csv
import pandas as pd
import re
with open (filename, "r") as file:
df = pd.read_csv(file, delimiter = ",")
df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
print(df.columns)
add a comment |
up vote
0
down vote
up vote
0
down vote
The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':
You could also try:
import csv
import pandas as pd
import re
with open (filename, "r") as file:
df = pd.read_csv(file, delimiter = ",")
df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
print(df.columns)
The key error generally comes if the key doesn't match any of the dataframe column name 'exactly':
You could also try:
import csv
import pandas as pd
import re
with open (filename, "r") as file:
df = pd.read_csv(file, delimiter = ",")
df.columns = ((df.columns.str).replace("^ ","")).str.replace(" $","")
print(df.columns)
answered Oct 20 at 22:53
beta
65
65
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f35831496%2fkey-error-when-selecting-columns-in-pandas-dataframe-after-read-csv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown