Python 将数据帧中的多列展平为一列

Python 将数据帧中的多列展平为一列,python,pandas,dataframe,Python,Pandas,Dataframe,我有这样一个数据帧: id other_id_1 other_id_2 other_id_3 1 100 101 102 2 200 201 202 3 300 301 302 to_keep = {} for idx in df.index: identifier = df.loc[idx]['id'] to_kee

我有这样一个数据帧:

id    other_id_1    other_id_2    other_id_3
1     100           101           102
2     200           201           202
3     300           301           302
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)
我想要这个:

id    other_id
1     100
1     101
1     102
2     200
2     201
2     202
3     300
3     301
3     302
我可以像这样轻松地获得所需的输出:

id    other_id_1    other_id_2    other_id_3
1     100           101           102
2     200           201           202
3     300           301           302
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)
这就给了我:

{1: [100, 101, 102], 2: [200, 201, 202], 3: [300, 301, 302]}

我可以轻松地将其写入文件。然而,我很难在本地熊猫身上做到这一点。我想这种看起来更简单的换位方式,但我正在努力…

好吧,如果您还没有,请将
id
设置为索引:

>>> 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
然后,您可以简单地使用
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
如果需要对值进行排序:

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

如果
id
不是索引,请先设置它:

df = df.set_index('id')

df

    other_id_1  other_id_2  other_id_3
id                                    
1          100         101         102
2          200         201         202
3          300         301         302
现在,调用
pd.DataFrame
构造函数。您必须使用
np平铺索引。重复

df_new = pd.DataFrame({'other_id' : df.values.reshape(-1,)}, 
                         index=np.repeat(df.index, len(df.columns)))
df_new

    other_id
id          
1        100
1        101
1        102
2        200
2        201
2        202
3        300
3        301
3        302
通过使用:

unstack

df.set_index('id').unstack().reset_index().drop('level_0',1).rename(columns={0:'other_id'})

Out[43]: 
   id  other_id
0   1       100
1   2       200
2   3       300
3   1       101
4   2       201
5   3       301
6   1       102
7   2       202
8   3       302
还有一个(或者更确切地说是两个):)

备选案文2:

df.set_index('id').stack().reset_index(1,drop = True).reset_index()\ 
.rename(columns = {0:'other_id'})
两种方法都可以

    id  other_id
0   1   100
1   1   101
2   1   102
3   2   200
4   2   201
5   2   202
6   3   300
7   3   301
8   3   302

我看你用了你最喜欢的