Python Forum
Reformat csv data with Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Reformat csv data with Python (/thread-11732.html)



Reformat csv data with Python - ksdst1 - Jul-23-2018

Hello. I am a newbie to python taking a data analytics intro class. I need to reconfigure the data from a UN imported csv file to resemble my other csv files, e.g., cols 'Country or Area', 'Year', 'Value' with rows first grouped by country and then sorted by year decreasing and the values (HDI value) during those years.

The UN data csv I need to covert is configured cols 'Counrty or Area','1990','1991','1992'...'2014', 'HDI Rank'. The rows contains each country name and the values for each col year, and lastly the overall HDI Rank value.

I am using jupyter Projects notebook and don't know the code to manipulate the imported UN csv file data so that I can export a csv that has cols 'Country or Area', 'Year', 'Value', where the rows are sorted first by Country name (duplicated downward), then years descending, and then values for those years.

Help in the right direction is much appreciated!


RE: Reformat csv data with Python - Larz60+ - Jul-23-2018

what the heck is an UN CSV file, how does it differ from a CSV file?

Now for the code. We won't do your homework for you, you need to make an effort, then we will help with problems as you encounter them.


RE: Reformat csv data with Python - buran - Jul-23-2018

(Jul-23-2018, 08:58 PM)Larz60+ Wrote: what the heck is an UN CSV file
I guess a csv file from UN (United Nations) statistical database :-)


RE: Reformat csv data with Python - ksdst1 - Jul-24-2018

Correct, UN is United Nations. Sorry, I should have been more clear. This is for a project in an intro class where I want to go beyond what we have covered/are responsible for. I don't need all the code, just need some guidance in regard to what libraries to use, e.g., panda re-index?? I can research how to use the libraries but just not sure were to start.
Thanks...


RE: Reformat csv data with Python - Larz60+ - Jul-24-2018

A csv file is text, so can be read like a text file.
But it's better to read as a csv.reader object as each line will be split into a list
to use csv package, you do something like:
import csv

def read_file(filename):
    with open(filename, 'r') as f:
        reader = csv.reader(f)
        for row in reader:
            for col in row:
                print('{0:<15} '.format(col), end='')
            print()

if __name__ == '__main__':
    read_file('myfile.csv')
if the first row contains headers, you can skip it, or use it for format the data into a dictionary or some other structure.


RE: Reformat csv data with Python - ksdst1 - Jul-25-2018

Thanks for the response. Someone else pointed me to the stack() function which did the trick for the inexperienced newbie.


RE: Reformat csv data with Python - Larz60+ - Jul-25-2018

stack function? please explain how this reads a CSV file.


RE: Reformat csv data with Python - ksdst1 - Jul-25-2018

I can pd.read_csv() files into a df just fine, but needed to reformat the df so it had the same formatting as the other dfs I had by importing the other csv files I'm working with.
Thanks


RE: Reformat csv data with Python - Larz60+ - Jul-25-2018

that doesn't explain stack function, but nevermind