python


How to do a group by operation on a list of json objects in python?


I am working on a Python script where I need to group by a key in a list of JSON objects.
I’ve a list of a large number of JSON objects in python in the following format:
[{'name': xyz,
'territory': abc,
'parameter_a': 1,
'parameter_b': 2,
'parameter_c': 3},
…]
Now I want to create a tag (say parameter_d) which should say the number in the grouped by count the number of times a specific territory (Say ‘abc’) has occurred in the whole list of json objects.
E.g. Territory abc occurs 3 times in the list, so i want the parameter_d to hold values 1,2,3 for the different instances where the territory abc occurred.
Thanks in advance for the help.
from json import loads, dumps
from collections import defaultdict
json_string = """
[
{"name": "xyz", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "qrs", "territory": "def", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "tuv", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "abc", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3}
]"""
# Step 1: convert from JSON to Python:
python_object = loads(json_string)
# Step 2: Add `parameter_d` to each item in list,
# using defaultdict(int) as a counter:
counts = defaultdict(int)
for item in python_object:
counts[item["territory"]] += 1
item["parameter_d"] = counts[item["territory"]]
# Step 3: convert from Python to JSON
json_string = dumps(python_object, indent=2)
print json_string
Using this part of Rob's answer for the setup:
from json import loads, dumps
from collectins import defaultdict
json_string = """
[
{"name": "xyz", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "qrs", "territory": "def", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "tuv", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3},
{"name": "abc", "territory": "abc", "parameter_a": 1, "parameter_b": 2, "parameter_c": 3}
]"""
# Step 1: convert from JSON to Python:
python_object = loads(json_string)
You could create a dictionary of territories, and then update your object:
territories = defaultdict(list)
for i, item in enumerate(python_object):
territories[item['territory']].append(i)
for item in python_object:
item['parameter_d'] = territories[item['territory']]
print(dumps(python_object, indent=2))

Related Links

matplotlib does not display latex fonts correctly
How to use py2exe with third-party libraries
How to extract two numbers from input line and tab delimit them?
Copy selected lines of text from one file to another with command line argument with or without spaces
TypeError: can't use a string pattern on a bytes-like object
Why does my code skip to the end and ignore parts of my code?
Matplotlib scroll_event not working with GTK3Agg backend
SELECT conditions on relations with SQLAlchemy
longest and shortest curves selected
Get the key with the maximum nested key in a python dictionary
Python fermat program only works for low numbers
How to create list of all parts of elements in NumPy array (Python list)
Encoding issue for Python tool Unidecode on CL
Accept only numbers on a python string [closed]
scrapy spider pass parameters
sending data to particular client from twisted server

Categories

HOME
tensorflow
erlang
redis
ncurses
bing
cpu-architecture
otrs
orientation
snap.svg
tizen-wearable-sdk
proguard
codeblocks
requirejs
xcode8.3
goutte
structuremap
aruco
click
cortex-a
xul
intellij-plugin
cs-cart
rapidjson
aspell
rhapsody
pyyaml
internet-explorer-8
attask
android-fragmentactivity
qsub
pentaho-report-designer
xilinx-ise
multichoiceitems
vertex-buffer
positioning
twitter-bootstrap-2
apache-cloudstack
canvasjs
division
rhino
remote-server
wfp
mms
meta-raspberrypi
alchemy.js
ddms
android-maps-v2
bids
double-buffering
uiviewpropertyanimator
ti-basic
tiddlywiki
openh264
photon-controller
keycode
master
activeweb
children
firepath
zurb-foundation-apps
nofollow
inotifypropertychanged
emokit
selecteditem
qt-linguist
web-essentials
hyperthreading
eyeql
jericho-html-parser
apigee-baas
paypal-nvp
visual-studio-code
swift2.1
shift-jis
es2015
uptodate
sysfs
magento-1.12
temp-tables
adcolony
mfmailcomposeviewcontroll
data-generation
jstack
visualstatemanager
unison
initialization-vector
visual-studio-addins
opendata
cilk-plus
installshield-2011
haskell-platform
symphony-cms
digital-design
qtgui
zend-framework-modules
qsqltablemodel
redirectstandardoutput
jquery-selectbox
throttling
smooth
for-xml-path
webrat
multibyte-functions
document-library

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App