将两个列大熊猫的值合并为一个

时间:2016-08-19 23:15:13

标签: python pandas

我有数据,并且必须使用pandas进行处理,这是代码示例:

d = [
  (1,70399,0.988375133622),
  (1,33919,0.981573492596),
  (1,62461,0.981426807114),
  (579,1,0.983018778374),
  (745,1,0.995580488899),
  (834,1,0.980942505189)
]

df = pd.DataFrame(d, columns=['source', 'target', 'weight'])
source_old = df.source.copy()

>>> source_old
source_old
0      1
1      1
2      1
3    579
4    745
5    834

但我也需要source_old包含target,例如:

0      1
1      1
2      1
3    579
4    745
5    834
6    33919
7    62461
8    70399

我怎么能用熊猫做到这一点? THX

3 个答案:

答案 0 :(得分:2)

df[['source', 'target']].T.stack()

source  0        1
        1        1
        2        1
        3      579
        4      745
        5      834
target  0    70399
        1    33919
        2    62461
        3        1
        4        1
        5        1
dtype: int64

<强>

pd.concat([df[col] for col in ['source', 'target']])

0        1
1        1
2        1
3      579
4      745
5      834
0    70399
1    33919
2    62461
3        1
4        1
5        1
dtype: int64

<强>

准确地得到你的要求

pd.concat([df['source'], df['target'].iloc[:3]], ignore_index=True)

0        1
1        1
2        1
3      579
4      745
5      834
6    70399
7    33919
8    62461
dtype: int64

答案 1 :(得分:2)

试试这个source_old = df.source.append(df.target)

答案 2 :(得分:1)

试试这个:

pd.melt(df[['source', 'target']])['value'][:-3].sort_values()

0        1
1        1
2        1
3      579
4      745
5      834
7    33919
8    62461
6    70399

Melt给你这个:

pd.melt(df[['source', 'target']])

  variable  value
0    source      1
1    source      1
2    source      1
3    source    579
4    source    745
5    source    834
6    target  70399
7    target  33919
8    target  62461
9    target      1
10   target      1
11   target      1