简体   繁体   English

如何拆分单列的数据以成对分隔列

[英]How to split data of single column to separate columns in pairs

I am using a csv file with dataframe name content .我正在使用数据框名称content的 csv 文件。 I am trying this code but it not providing expected output我正在尝试这段代码,但它没有提供预期的输出

new_content1 = content['Value1','Value2','Value3','Value4','Value5','Value6']
def get_pairs(x):
    arr = x.split(' ')
    return list(map(list, zip(arr, arr[1:])))

new_content1['pairs'] = new_content1.applymap(get_pairs)
new_content1

Value1 Value2 .... pairs(single column) Value1 Value2 .... 对(单列)
0 3 2 2 4 2 2 [[3, 2], [2, 2], [2, 4], [4, 2], [2, 2]] 1 1 2 3 4 5 6 [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]] 0 3 2 2 4 2 2 [[3, 2], [2, 2], [2, 4], [4, 2], [2, 2]] 1 1 2 3 4 5 6 [[1, 2] ], [2, 3], [3, 4], [4, 5], [5, 6]]

IIUC, you can simply use: IIUC,您可以简单地使用:

new_content1['pairs'] = new_content1['ColName'].str.split(" ", n = 1, expand = True)

where ColName is column you want to split, it will return a values as list in pairs column其中 ColName 是您要拆分的列,它将返回一个值作为pairs列中的列表

Alternatively, if you want the output to make new columns based on splits, eg two columns, you can use the above as:或者,如果您希望输出基于拆分创建新列,例如两列,则可以将上述内容用作:

new_content1[['newCol1', 'newCol2']] = new_content1['ColName'].str.split(" ", n = 1, expand = True)

As per your comment let say you have df column like this one:根据您的评论,假设您有这样的df列:

Value
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

if your column are separate then join them using:如果您的列是分开的,则使用以下方法加入它们:

df['Value']=df['value1']+' '+df['value2']+' '+df['value3']+' '+df['value4']+' '+df['value5']+' '+df['value6']

then you can do this那么你可以这样做

df['Value']=df['Value'].astype('str')

df['new']=df['Value'].str.split(' ').apply(lambda x:list((zip(x[:], x[1:]))))

Output looks like this输出看起来像这样


+--------+--------------+------------------------------------------+
| Value  |     new      |                pairs                     |
+--------+--------------+------------------------------------------+
|     0  | 1 2 3 4 5 6  | [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
|     1  | 1 2 3 4 5 6  | [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
|     2  | 1 2 3 4 5 6  | [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
|     3  | 1 2 3 4 5 6  | [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
|     4  | 1 2 3 4 5 6  | [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
|     5  | 1 2 3 4 5 6  | [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
|     6  | 1 2 3 4 5 6  | [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] |
+--------+--------------+------------------------------------------+

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

相关问题 将单列拆分为 Dataframe 中的 4 个不同的单独列 - Split the single column to 4 different separate columns in Dataframe 如何在 Python 中将列中的数据拆分为一些单独的列? - How to split data in a column into some separate columns in Python? 如何拆分混合数据列以分隔 python 中的 int 和 str 列 - how to split a mixed data column to separate int and str columns in python 如何在熊猫中将单列数据拆分为多列? - how to split a single column data into muliple columns in pandas? 如何将串联的列名称拆分为单独的列? - How to split concatenated column name into separate columns? Python / Pandas:将单列中的美元值拆分为单独的列 - Python / Pandas: Split Dollar Values in a Single Column to Separate Columns 如何将 3 个单独的列转换为单个日期列? - How to convert 3 separate columns into a single date column? 取熊猫数据框中的字符串数据列并拆分为单独的列 - Take column of string data in pandas dataframe and split into separate columns 基于值将数据从一列拆分为单独的列 - Split Data From One Column Into Separate Columns Based On Value 如何从文本文件中拆分数据并在特定的 position 通过为每列提供标题来创建单独的列 - how to split data from text file and create separate columns at a specific position by giving title for each column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM