Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Code to Reformat New Format to Older Format #1458

Open
hippodribble opened this issue Mar 24, 2020 · 2 comments
Open

Python Code to Reformat New Format to Older Format #1458

hippodribble opened this issue Mar 24, 2020 · 2 comments

Comments

@hippodribble
Copy link

@hippodribble hippodribble commented Mar 24, 2020

I see they changed the format. The following code puts a "new" file into the "old" format.

Process

  1. Read the file
  2. Swap and rename columns
  3. Write the new file

The function writes to the same folder with a 'b' in the filename as the last character before the ".csv"

Call it with the full file path of the file in the "new" format

Cheers

Glenn

import os.path
import pand as pd
def reformat(infile):
    (dir, file) = os.path.split(infile)
    f = file.replace(".csv","b.csv")
    outfile = os.path.join(dir,f)
    print(infile)
    print(outfile)
    df1 = pd.read_csv(infile, parse_dates=[4],skip_blank_lines=False )
    dic = {
        "Province/State":"Province_State",
        "Country/Region":"Country_Region",
        "Last Update":"Last_Update",
        "Confirmed":"Confirmed",
        "Deaths":"Deaths",
        "Recovered":"Recovered",
        "Latitude":"Lat",
        "Longitude":"Long_"
    }
    temp=df1[list(dic.values())]
    temp.columns=list(dic.keys())
    temp.reset_index(drop=True, inplace=True)
    temp.to_csv(outfile, index=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants