简体   繁体   English

将数据从一列分为三列

[英]Separate data from one column into three columns

I have a column in an excel which contains a mix of First Names, Last Names and Job titles. 我在excel中有一列,其中包含名字,姓氏和职务的混合。 Only pattern that can be observed is - in each set of 3 rows, every 1st row is first name, 2nd row is last name and 3rd row is job title. 唯一可以观察到的模式是-在每三行中,第一行是名字,第二行是姓氏,第三行是职位。 I want to create 3 different columns and and segregate this data Sample data: 我想创建3个不同的列,并隔离此数据示例数据:

John
Bush
Manager
Katrina
Cohn
Secretary 

I want: John , Bush , Manager as one row going in three different columns under First Name, Last name and Job title respectively. 我想要:John,Bush,Manager作为一行,分别在First Name,Last name和Job title下的三个不同的列中。 Like - 喜欢 -

First Name   Last Name    Job Title
John         Bush         Manager
Katrina      Cohn         Secretary 

How can we achieve this task? 我们如何完成这项任务?

You can use this notation to get every third element with different starting points. 您可以使用此表示法来获取每三个具有不同起点的元素。

l = ['John', 'Bush', 'Manager', 'Katrina', 'Cohn', 'Secretary']

pd.DataFrame({'First Name': l[::3], 'Last Name': l[1::3], 'Job Title': l[2::3]})

outputs 输出

  First Name  Job Title Last Name
0       John    Manager      Bush
1    Katrina  Secretary      Cohn
s = pd.Series([
        'John',
        'Bush',
        'Manager',
        'Katrina',
        'Cohn',
        'Secretary'])

df = pd.DataFrame(s.values.reshape(-1, 3),
                  columns=['First Name', 'Last Name', 'Job Title'])

df

在此处输入图片说明


If your length of your data isn't a multiple of 3 then you can force it like this: 如果您的数据长度不是3的倍数,则可以这样强制:

s = pd.Series([
        'John',
        'Bush',
        'Manager',
        'Katrina',
        'Cohn',
        'Secretary',
        'Bogus'])

s_ = s.iloc[:s.shape[0] // 3 * 3]
df = pd.DataFrame(s_.values.reshape(-1, 3), columns=['First Name', 'Last Name', 'Job Title'])

df

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于值将数据从一列拆分为单独的列 - Split Data From One Column Into Separate Columns Based On Value 如何将一列中的数据放入单独的列中 - How do I put data from one column into separate columns My.txt 文件作为一列读入我的笔记本,而不是三个单独的列 - My .txt file reads into my notebook as one column as opposed to three separate columns 当一列与其他列分开时,如何 select DataFrame 列? - How to select DataFrame columns when one column is separate from the others? 从具有最多数据(非 NA)的三列创建组合列 - Create Combined Column From Three Columns With The Most Data (Not NA) 将一列中的数据作为单独的行连接到另一列 - Join data from one column in to another column as a separate row 从 DataFrame 列中提取字符串数据以分隔列 - Extract String Data From DataFrame Column To Separate Columns 如何在python中将一列分成多列? - How to separate one column into multiple columns in python? 从一个字符串列创建两列,一列使用前三个元素,另一列使用.format() - From one string column creates two columns, one using the first three elements, and the other using .format() 如何从一列中提取值并根据 python 中的目标列创建单独的二进制列 - How to extract values from one column and create separate binary columns based on the targeted column in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM