So, I have a pandas dataframe like this.
mac_address City
0 00:03:7f:05:c0:06 Kolkata
1 00:08:22:1c:50:07 Bhubaneswar
2 00:08:22:1c:50:07 Mumbai
3 00:08:22:1c:50:07 Mumbai
4 00:08:22:1c:50:07 Kolkata
5 00:08:22:24:cc:fb Bhubaneswar
6 00:08:22:24:f8:02 Mumbai
7 00:08:22:24:f8:02 Kolkata
8 00:08:22:24:f8:02 Mumbai
9 00:08:22:24:f8:02 Bhubaneswar
Now the unique key here is mac_address so I want to start with a empty JSON document. for that I will start with a dictionary in python which later I can dump into JSON . I dont know how to start with empty dict(you can help with that too) so, I have started with one value. Now for each new row of data frame if the mac_address which is also is the index of dict if mac_id is there update the corresponding city and city count. And if it is not there add a new field(if it is called field) with index as the new mac_address and store the value accordingly.This is the dictionary to start with.
data = {"00:08:22:24:f8:02": {
"mac_address" : "00:08:22:24:f8:02",
"cities" :
[
{'name': 'Bhubaneswar', 'count': 12},
{'name': 'Kolkata', 'count': 4},
{'name': 'Mumbai', 'count': 6}
]
}
}
city count is no. of times a mac_address visited to a city. By reading this particular row I would like to update a city named Bhubneswar and Count 1.
Update The question here is to how to update a dictionary directly from a data frame row by row. Which I somehow failed to explain. This update might help people to understand.