pandas :多栏成一栏 [英] Pandas: Multiple columns into one column

查看:70
本文介绍了 pandas :多栏成一栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据(2列4行):

I have the following data (2 columns, 4 rows):

Column 1: A, B, C, D

Column 2: E, F, G, H

我正在尝试将各列合并为一列,看起来像这样(1列,8行):

I am attempting to combine the columns into one column to look like this (1 column, 8 rows):

Column 3: A, B, C, D, E, F, G, H

我正在使用pandas DataFrame,并尝试使用其他功能但没有成功(appendconcat等).任何帮助将不胜感激!

I am using pandas DataFrame and have tried using different functions with no success (append, concat, etc.). Any help would be most appreciated!

推荐答案

更新

pandas为此 stack ,您会看到其他

pandas has a built in method for this stack which does what you want see the other answer.

这是我多年前认识stack之前的第一个答案:

This was my first answer before I knew about stack many years ago:

In [227]:

df = pd.DataFrame({'Column 1':['A', 'B', 'C', 'D'],'Column 2':['E', 'F', 'G', 'H']})
df
Out[227]:
  Column 1 Column 2
0        A        E
1        B        F
2        C        G
3        D        H

[4 rows x 2 columns]

In [228]:

df['Column 1'].append(df['Column 2']).reset_index(drop=True)
Out[228]:
0    A
1    B
2    C
3    D
4    E
5    F
6    G
7    H
dtype: object

这篇关于 pandas :多栏成一栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆