0

I have some data in an Excel file. I would like to analyze them using Python. I started by creating a CSV file using this guide.

Thus I have created a CSV (Comma delimited) file filled with the following data:

Small amount of data I would like to analyze

I wrote a few lines of code in Python using Spyder:

import pandas
colnames = ['GDP', 'Unemployment', 'CPI', 'HousePricing']
data = pandas.read_csv('Dane_2.csv', names = colnames)
GDP = data.GDP.tolist()
print(GDP)

The output is nothing I've expected:

The wrong output

It can be easily seen that the output differs a lot from the figures in GDP column. I will appreciate any tips or hints which will help to deal with my problem.

2
  • Might be worth incuding the first few lines of your CSV as it appears in a text editor. Commented Sep 20, 2018 at 12:50
  • 1
    This is a common issue when you're working with European decimal notation. CSVs can be an problem unless you by default import/export using defined separators like the semicolon. So consider this going forward. Commented Sep 20, 2018 at 12:53

1 Answer 1

1

Seems like in the GDP column there are decimal values from the first column in the .csv file and first digits of the second column. There's either something wrong with the .csv you created, but more probably you need to specify separator in the pandas.read_csv line. Also, add header=None, to make sure you don't lose the first line of the file (i.e. it will get replaced by colnames).

Try this:

import pandas
colnames = ['GDP', 'Unemployment', 'CPI', 'HousePricing']
data = pandas.read_csv('Dane_2.csv', names = colnames, header=None, sep=';')
GDP = data.GDP.tolist()
print(GDP)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I think it worked. Is there an "easy" way to change strings into floats?
sure, use: data['column name'] = data['column name'].astype(float). Also, if it helped you, do you mind accepting the answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.