简体   繁体   English

如何使用python拆分列

[英]how to split column using python

My input is as below我的输入如下

Column A A栏
ABC-5678-1/15/1 ABC-5678-1/15/1
ABCD-7589-1/8/2 ABCD-7589-1/8/2
AB-78698-1/10/1 AB-78698-1/10/1

and required output is (only want to taken value between two /)并且所需的输出是(只想在两个 / 之间取值)

Column A A栏
ABC-5678-15 ABC-5678-15
ABCD-7589-8 ABCD-7589-8
AB-78698-10 AB-78698-10

You can use str.replace with a regex, capturing groups, and references:您可以将str.replace与正则表达式、捕获组和引用一起使用:

df['Column A'].str.replace(r'^([^/]+-)[^-/]+/([^/]+)/.*', r'\1\2', regex=True)

Output:输出:

0    ABC-5678-15
1    ABCD-7589-8
2    AB-78698-10

I would use str.replace as follows:我会使用str.replace如下:

df['Column A'] = df['Column A'].str.replace(r'/\d+$', '')

Here is a demo showing that the regex replacement is working.这是一个演示,显示正则表达式替换正在工作。

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

相关问题 如何使用python将大文本文件中的列拆分为3列? - How to split column into 3 column in large text file using python? 如何使用 python 将列中的列表拆分为 dataframe 中的两列? - How to split a list in a column into two column in a dataframe using python? 如何使用 python 将 excel 的一列中的数据拆分为多列 - How to Split data in one column of excel into multiple column using python Python,使用数据框如何在列中拆分字符串的值,然后使用拆分后的值添加新列 - Python,using dataframes how to split the value of a string in a column and then add a new column with the value from the split Python-如何使用多个分隔符拆分列值 - Python - How to split column values using multiple separators 如何在 Python 中使用 str.split() 过滤出一行和一列 - How to filter out a row and column using str.split() in Python 如何使用python从csv中的同一列拆分日期和时间? - how to split date and time from same column in csv using python? 如何使用python根据一列将整个数据集分为4个范围 - How to split the whole dataset into 4 range based on one column using python 如何使用python将日期列拆分为日/月/年列 - How to split a date column into day/month/year columns using python 如何使用 python 将列的内容拆分为 csv 文件中的不同列? - How to split contents of column into different columns in csv files using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM