开发者

Create a json file based on data of the two given column in a csv which needs to be mapped together in python [closed]

开发者 https://www.devze.com 2022-12-07 21:43 出处:网络
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code nece
Closed. This question needs debugging details. It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary开发者_C百科 to reproduce the problem. This will help others answer the question.

Closed 3 hours ago.

Improve this question
Column_A Column_B
Ram Menu
Ram Radha
Shyam Rishi
Rishi Ram
Rishi Ram
Menu Ram

SO i have a csv like this now what i have to i get a json which looks like this. Keys: these are the unique value of each of the entires in the csv columns except the columns headers. pairs: just like it suggests if Ram pairs with menu and Ram also pairs with Radha them "Ram" : ["Meenu","Radha"]

{
    "keys": {
        "Ram" :1,
        "Shyam" :2,
        "Rishi": 3,
        "Menu": 4,
        "Radha": 5,
        "Shyam": 6
        },
    "pairs": {
        "1" : [4,5],
        "2" : [3],
        "3" : [1],
        "4" : [1],
        "5" : []
        }
}


I agree with some of the comments that the question could have been framed a little more clearly, but I think I may have been able to interpret what you were looking for based on your input and output.

See if this does what you are looking for.

import pandas as pd
import json

df = pd.DataFrame([
    {"Column_A":"Ram", "Column_B":"Menu"},
    {"Column_A":"Ram", "Column_B":"Radha"},
    {"Column_A":"Shyam", "Column_B":"Rishi"},
    {"Column_A":"Rishi", "Column_B":"Ram"},
    {"Column_A":"Rishi", "Column_B":"Ram"},
    {"Column_A":"Menu", "Column_B":"Ram"},
])

a = pd.concat([df["Column_A"], df["Column_B"]]).unique()
keys = {v:i+1 for (i,v) in enumerate(a)}
pairs = df.replace(keys).groupby("Column_A").apply(lambda df: [int(v) for v in list(set(df["Column_B"].values))]).to_dict()

result = {
    "keys":keys,
    "pairs":pairs
}

print(json.dumps(result))

Output:

{"keys": {"Ram": 1, "Shyam": 2, "Rishi": 3, "Menu": 4, "Radha": 5}, "pairs": {"1": [4, 5], "2": [3], "3": [1], "4": [1]}}

If, as in the comments, you are looking to create your DataFrame from a string variable, you can include something like this:

from os import StringIO
pd.read_csv(StringIO("A,B,C\n1,2,3\n4,5,6"))
0

精彩评论

暂无评论...
验证码 换一张
取 消