0

I have a function that takes user input from the user for every wrong column name in a dataset.

For example, if the master dataset has column names X, Y, Z but the child dataset has column names as A, B, C. I made user input the correct name to match it to the master. I save every wrong name as a key and right name as a value in a dictionary like this:

    {"A":"X", "B":"Y", "C":"Z"}

I save that dictionary as a json file. I don't want the user to input the same name they have already entered earlier when the code runs again later and it should take reference from the dictionary we created earlier.

I am able to create the dictionary in first run but I don't know how to check the dictionary in next run and how to take reference from the dictionary first.

Ankit Sati
  • 33
  • 7
  • Possible duplicate of [How do I write JSON data to a file?](https://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file) – mad_ Aug 30 '18 at 18:17
  • Can you add an example that walks through the steps you're describing? It's not quite clear what your issue is. – ZaxR Aug 31 '18 at 01:42

1 Answers1

1

This is a full example. The first part is writing your dictionary to a JSON file. The second part is when you want to load that JSON file and rename your columns later.

import json
import pandas as pd

json_file = "/Users/jkornblum/Desktop/cols.json"
df = pd.DataFrame(columns=["A", "B", "C"])

# You would return col_map from your function that accepts user input
col_map = {"A":"X", "B":"Y", "C":"Z"}

# Write column map to disk
with open(json_file, 'w') as outfile:
    json.dump(col_map, outfile)

# Read the column map and apply
col_map = None # Clear col_map for example
with open(json_file, 'r') as infile:
    col_map = json.load(infile)

# rename the dataframe columns
df = df.rename(columns=col_map)
Josh
  • 2,767
  • 1
  • 27
  • 31