Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas merge two columns into Json

Tags:

pandas

I have a pandas dataframe like below

    Col1    Col2
0     a     apple
1     a     anar
2     b     ball
3     b     banana

I am looking to output json which outputs like

{ 'a' : ['apple', 'anar'], 'b' : ['ball', 'banana'] }
like image 936
garg10may Avatar asked Mar 02 '19 08:03

garg10may


People also ask

How to merge two pandas DataFrames on multiple columns?

How to Merge Pandas DataFrames on Multiple Columns Often you may want to merge two pandas DataFrames on multiple columns. Fortunately this is easy to do using the pandas merge()function, which uses the following syntax:

How do you combine data in a Dataframe?

You have now learned the three most important techniques for combining data in Pandas: merge () for combining data on common columns or indices. .join () for combining data on a key column or an index. concat () for combining DataFrames across rows or columns.

How to merge multiple JSON files in a folder with Python?

Merging multiple files requires several Python libraries like: pandas, glob, os and json. Next we can see how to list JSON files in a folder with Python: Note: for JSON lines you may need to change the matching pattern to '*.jl'

How do I join two Dataframe columns in Python?

If you want to join on columns like you would with merge(), then you’ll need to set the columns as indices. Like merge(), .join() has a few parameters that give you more flexibility in your joins. However, with .join(), the list of parameters is relatively short: other: This is the only required parameter. It defines the other DataFrame to join.


Video Answer


2 Answers

Use groupby with apply and last convert Series to json by Series.to_json:

j = df.groupby('Col1')['Col2'].apply(list).to_json()
print (j)
{"a":["apple","anar"],"b":["ball","banana"]}

If want write json to file:

s = df.groupby('Col1')['Col2'].apply(list)
s.to_json('file.json')

Check difference:

j = df.groupby('Col1')['Col2'].apply(list).to_json()
d = df.groupby('Col1')['Col2'].apply(list).to_dict()

print (j)
{"a":["apple","anar"],"b":["ball","banana"]}
print (d)
{'a': ['apple', 'anar'], 'b': ['ball', 'banana']}

print (type(j))
<class 'str'>

print (type(d))
<class 'dict'>
like image 113
jezrael Avatar answered Oct 01 '22 01:10

jezrael


You can groupby() 'Col1' and apply() list to 'Col2' and convert to_dict(), Use:

df.groupby('Col1')['Col2'].apply(list).to_dict()

Output:

{'a': ['apple', 'anar'], 'b': ['ball', 'banana']}
like image 34
anky Avatar answered Oct 01 '22 01:10

anky