pandas :多栏成一栏

编程入门 行业动态 更新时间:2024-10-28 15:28:47
本文介绍了 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,并尝试使用其他功能但没有成功(append,concat等).任何帮助将不胜感激!

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 :多栏成一栏

本文发布于:2023-10-14 19:01:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1491924.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:pandas   多栏成

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!