I have a data frame below:
user | speed
------------
Anna | 1.0
Bell | 1.2
Anna | 1.3
Chad | 1.5
Bell | 1.4
Anna | 1.1
that I want to use a dictionary to keep record of the number of encounters for each user and update his/her speed when I loop through the data frame.
For instance, the first time we see "Anna" the dictionary is:
{"Anna": [1, 1.0]}
and the second time we see "Anna" it becomes:
{"Anna": [2, 1.3], "Bell": [1, 1.2]}
in the end the dictionary should be:
{"Anna": [3, 1.1], "Bell": [2, 1.4], "Chad": [1, 1.5]}
The counting part is easy:
>>> import pandas as pd
>>> record = pd.DataFrame({"user": ("Anna", "Bell", "Anna", "Chad", "Bell", "Anna"), "speed": (1.0, 1.2, 1.3, 1.5, 1.4, 1.1)})
>>> record
speed user
0 1.0 Anna
1 1.2 Bell
2 1.3 Anna
3 1.5 Chad
4 1.4 Bell
5 1.1 Anna
>>> encounter = {}
>>> for i in record['user']:
... encounter[i] = encounter.get(i, 0) + 1
...
>>> encounter
{'Anna': 3, 'Bell': 2, 'Chad': 1}
but what's a good way to create an empty dictionary of list and update the second value? Thanks!
I believe this is what you want in two lines.
import pandas as pd
record = pd.DataFrame({
"user": ("Anna", "Bell", "Anna", "Chad", "Bell", "Anna"),
"speed": (1.0, 1.2, 1.3, 1.5, 1.4, 1.1)
})
encounter = {}
for name, value in zip(record["user"], record["speed"]):
encounter[name] = [encounter.get(name, [0])[0] + 1, value]
zip
method let you loop through the names and speeds simultaneously. get
method try gets the record if it exists, else return a list [0]
. [0]
takes the first element of the list, which is the counter. encounter[name]
. Using collections.Counter
Ex:
import pandas as pd
from decimal import Decimal
from collections import Counter
record = pd.DataFrame({"user": ("Anna", "Bell", "Anna", "Chad", "Bell", "Anna"), "speed": (1.0, 1.2, 1.3, 1.5, 1.4, 1.1)})
encounter = {}
for k,v in Counter(record["user"].tolist()).items():
encounter[k] = [v, (record[record["user"] == k]["speed"].iloc[-1]).round(1).astype(Decimal)]
print(encounter)
Output:
{'Anna': [3, 1.1], 'Chad': [1, 1.5], 'Bell': [2, 1.4]}
go with pandorable ,
my_dictionary={}
for k, v in df.groupby('user'):
my_dictionary[k]=[len(v),v.iloc[-1]['speed']]
print(my_dictionary)
{'Anna': [3, 1.1], 'Bell': [2, 1.4], 'Chad': [1, 1.5]}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.