Key error when selecting columns in pandas dataframe after read_csv
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 |
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 |
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
123k12122175
123k12122175
asked Mar 6 '16 at 19:30
Harry MHarry M
1442415
1442415
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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']
1
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
2
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 '18 at 11:03
This is AMAZING! Thank you so much!
– Sreehari R
Jan 25 at 23:09
add a comment |
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 |
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 |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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']
1
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
2
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 '18 at 11:03
This is AMAZING! Thank you so much!
– Sreehari R
Jan 25 at 23:09
add a comment |
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']
1
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
2
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 '18 at 11:03
This is AMAZING! Thank you so much!
– Sreehari R
Jan 25 at 23:09
add a comment |
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
MaxUMaxU
123k12122175
123k12122175
1
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
2
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 '18 at 11:03
This is AMAZING! Thank you so much!
– Sreehari R
Jan 25 at 23:09
add a comment |
1
You are amazing! Thanks so much!!
– Harry M
Mar 6 '16 at 19:45
2
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 '18 at 11:03
This is AMAZING! Thank you so much!
– Sreehari R
Jan 25 at 23:09
1
1
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
2
2
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 '18 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 '18 at 11:03
This is AMAZING! Thank you so much!
– Sreehari R
Jan 25 at 23:09
This is AMAZING! Thank you so much!
– Sreehari R
Jan 25 at 23:09
add a comment |
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 |
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 |
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 '18 at 5:22
answered Aug 15 '18 at 16:46
Bhaskar aryaBhaskar arya
65
65
add a comment |
add a comment |
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 |
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 |
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 '18 at 22:53
betabeta
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.
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