Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

python - Flatten multiple columns in a dataframe to a single column

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, if you haven't already, set id as the index:

>>> df
   id  other_id_1  other_id_2  other_id_3
0   1         100         101         102
1   2         200         201         202
2   3         300         301         302
>>> df.set_index('id', inplace=True)
>>> df
    other_id_1  other_id_2  other_id_3
id
1          100         101         102
2          200         201         202
3          300         301         302

Then, you can simply use pd.concat:

>>> df = pd.concat([df[col] for col in df])
>>> df
id
1    100
2    200
3    300
1    101
2    201
3    301
1    102
2    202
3    302
dtype: int64

And if you need the values sorted:

>>> df.sort_values()
id
1    100
1    101
1    102
2    200
2    201
2    202
3    300
3    301
3    302
dtype: int64
>>>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...