in Education by
I am trying to merge 2 data frames by using the id and then trying to save the result in the JSON file. | |id|a|b |1| 5|1|4 |2|10|2|5 |3|15|3|6 + | |id|a |b |1| 5|10|13 |2|10|11|14 |3|15|12|15 = | |id| a | b |1| 5|1, 10|4, 13 |2|10|2, 11|5, 14 |3|15|3, 12|6, 15 Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
Try the below code: df3 = pd.concat([df1, df2]).groupby('id', as_index=False).agg({'a': lambda x: ', '.join(map(str, x)), 'b': lambda x: ', '.join(map(str, x))}) print(df3) Output: id a b 0 5 1, 10 4, 13 1 10 2, 11 5, 14 2 15 3, 12 6, 15 you can use the below code to convert to json print(df3.to_json()) Output: { "id": { "0": 5, "1": 10, "2": 15 }, "a": { "0": "1, 10", "1": "2, 11", "2": "3, 12" }, "b": { "0": "4, 13", "1": "5, 14", "2": "6, 15" } } If you are a beginner and want to know more about Python the do check out the python for data science course

Related questions

0 votes
    I have a 20 x 4000 dataframe in python using pandas. Two of these columns are named Year and quarter. I'd ... anyone help with that? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have a big data frame. I want to replace float 1.0 and 0.0 with the true and false. My code: import pandas ... to do it in one line. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How do I use the for loop to execute the following operation? df_1=pd.concat([df_ab1,df_xy1], axis=1) df_2= ... Am I missing something? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    I have this dataframe and trying to select the last n (=2) rows if the present value is True so I code as ... , I should select 50,40 Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I am trying to create a new column where in, True if last n rows are True in other column. It works fine ... better way to achieve it? Select the correct answer from above options...
asked Jan 8, 2022 in Education by JackTerrance
0 votes
    I have two data frames with the closest matching DateTime index, sometimes matching. An object is to merge two of ... I achieve this? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I'm starting with input data like this df1 = pandas.DataFrame( { "Name" : ["Alice", "Bob", "Mallory", ... Any hints would be welcome. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with a column designated to town names. After each town name, I am adding a word " ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have the pandas data frame with the column designated to town names. After each town name, I am adding a word ... .csv', index=False) Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with 2 columns and I want to add the new column; This new column should be updated based on ... +=1 is not working. Select the correct answer from above options...
asked Jan 11, 2022 in Education by JackTerrance
0 votes
    I have 2 columns in the python dataframe. I want to check each row in my Column A for any value that ... for this particular purpose. Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I'm looking for a decent implementation of the OPTICS algorithm in Python. I will use it to form density-based ... to that cluster. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    I want to save files for each result from the loop. For example, I wrote the code but it only saves one file ... do I modify my code? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Here is my sample string: [true, {"name": "NameofItem", "amount": "1", "price": 100, "sellerName": " ... seem to make that work. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Hi, I am a relatively new programmer and my teacher gave us this problem to fix. Thing is, I have no idea ... wrong with this problem? Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
...