I have a dataframe like this:
id other_id_1 other_id_2 other_id_3
1 100 101 102
2 200 201 202
3 300 301 302
I want this:
id other_id
1 100
1 101
1 102
2 200
2 201
2 202
3 300
3 301
3 302
I can get my desired output easily like this:
to_keep = {}
for idx in df.index:
identifier = df.loc[idx]['id']
to_keep[identifier] = []
for col in ['other_id_1', 'other_id_2', 'other_id_3']:
row_val = df.loc[idx][col]
to_keep[identifier].append(row_val)
Which gives me this:
{1: [100, 101, 102], 2: [200, 201, 202], 3: [300, 301, 302]}
I can easily write that to a file. I am struggling to do this in native pandas, however. I would imagine this seeming transposition would be more straightforward, but am struggling...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…