I have a situation where I need to change some junk data in json such as
'a' need to be A
'B' need to be B
I want to create a config json which shall have a dictionary where the key & value should look like dict={'a':'A', 'b':'B'}
And then access the json in another python file which reads data from a dataframe where those junk values(keys of the dictionary) are there & change them to the correct ones(values of the dictionary). Can anyone help..?
CodePudding user response:
So, given the following config.json
file:
{
"junk1": "John",
"junk2": "Jack",
"junk3": "Tom",
"junk4": "Butch"
}
You could have the following python file in the same directory:
import pandas as pd
import json
with open("config.json", "r") as f:
cfg = json.load(f)
df = pd.DataFrame(
{
"class": {
0: "class1",
1: "class2",
2: "class3",
3: "class4",
},
"firstname": {0: "junk1", 1: "junk2", 2: "junk3", 3: "junk4"},
}
)
print(df)
# Outputs
class firstname
0 class1 junk1
1 class2 junk2
2 class3 junk3
3 class4 junk4
And then do:
df["firstname"] = df["firstname"].replace(cfg)
print(df)
# Outputs
class firstname
0 class1 John
1 class2 Jack
2 class3 Tom
3 class4 Butch