Issue
I have a dataframe with three columns: pid, idx and value, like this...
I need to convert it to a dictionary where the key is pid and the value is array of tuples(idx, value)
It should look something like this
1 2:0.5 3:0.12 10:0.9 2000:0.3
0 4:1.0 78:0.6 1009:0.2
1 33:0.1 34:0.98 1000:0.8 3300:0.2
1 34:0.1 389:0.32
I'm really stuck on this. Please help
Solution
Iterate on the rows, adding new entry row['pid']
to the dictionary at each iteration.
You can use d.setdefault(k, []).append(v)
to avoid a tedious if k in d: d[k].append(v) else: d[k] = [v]
.
import pandas as pd
df = pd.DataFrame({'pid': [78,78,78,99971,99971,99971], 'idx': [20,164,175,1451,1452,1453], 'value': [0.0,0.0,0.0,0.090909,0.090909,0.090909]})
d = {}
for i, row in df.iterrows():
d.setdefault(row['pid'], []).append((row['idx'], row['value']))
print(d)
# {78.0: [(20.0, 0.0), (164.0, 0.0), (175.0, 0.0)], 99971.0: [(1451.0, 0.090909), (1452.0, 0.090909), (1453.0, 0.090909)]}
Answered By - Stef
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.