Mortgage1
Mortgage1

Reputation: 35

Slicing in python issues

I am new to python so please be gentle. I am trying to take the values from the columns begin at 'Ball 1' to 'Ball Set' and create a new set called balls but I keep getting this error

KeyError: "None of [['Ball_1', 'Ball_Set']] are in the [columns]"

Here's my code

import pandas as pd

def read_csv(url):
   df = pd.read_csv(url,   sep='\t', na_values=".")
   return df

url ='https://www.national-lottery.co.uk/results/lotto/draw-history/csv'
df = read_csv(url) 

#splices file between the two arguments
Balls = df.loc[:,['Ball_1','Ball_Set']]
print(Balls)

I was expecting a print of all the data contained within the columns starting at Ball 1 and then continuing all the way ball set.

Upvotes: 0

Views: 70

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339795

The column names in the file are named Ball 1,Ball 2, ..., not Ball_1,Ball_2. Try removing the underscore.

Also, the separator is ,, not \t. So use sep=','.

Complete working code:

import pandas as pd

url ='https://www.national-lottery.co.uk/results/lotto/draw-history/csv'
df = pd.read_csv(url,   sep=',', na_values=".")

Balls = df.loc[:,['Ball 1','Ball Set']]
print(Balls)

Upvotes: 1

Related Questions