简体   繁体   English

Python:如何使用正则表达式拆分列中的值并保留右侧?

[英]Python: How do I split a value in column using regex and keep the right side?

I'm trying to clean up a column in my data frame that has the count next to size (ex: 12X350).我正在尝试清理我的数据框中的一个列,该列的计数位于大小旁边(例如:12X350)。 I'm trying to only get the right side of the X which is the size.我试图只得到 X 的右侧,即大小。 But having some issues.但是遇到了一些问题。

import pandas as pd 
data = [['product1', '13X255'], ['product2', "2"], ['product3', "500"]] 
df = pd.DataFrame(data, columns = ['Product', 'Size'])  
df 

    Product     Size
0   product1    13X255
1   product2    2
2   product3    500

Using this string split with regex gets at what I need but fills in values with将这个字符串与正则表达式一起使用可以满足我的需要,但会用

df['Size'].str.split(r'[A-Z]', n =1, expand = True)
    0   1
0   13  255
1   2   None
2   500 None

Desired output that I would like to have:我想要的所需 output :

    Product     Size
0   product1    255
1   product2    2
2   product3    500

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

Use利用

df['Right Side Of X'] = df['Size'].str.extract(r'(\d+)$', expand=False).fillna("")

The (\d+)$ expression will capture one or more digits at the end of string. (\d+)$表达式将捕获字符串末尾的一位或多位数字。 .fillna("") will populate non-matches with empty strings. .fillna("")将用空字符串填充不匹配项。

IIUC国际大学联盟

df['Size'] = df['Size'].str.split(r'[A-Z]', n=1).str[-1]
#if neccessary astype(str)
#df['Size'] = df['Size'].astype(str).str.split(r'[A-Z]', n=1).str[-1]

or或者

df['Size'] = df['Size'].str.split(r'[A-Z]', n=1, expand = True).ffill(axis=1)[1]

print(df)
    Product Size
0  product1  255
1  product2    2
2  product3  500

暂无
暂无

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

相关问题 如何在 Python 中使用正则表达式按括号拆分列表中的值? - How do I split values in a list by parenthesis using regex in Python? 如何使用 pandas 将左侧包含“=”的单元格内容拆分为列标题,将右侧拆分为行值 - How do I split cell contents that contain “=” the left part into column titles and the right into row values, using pandas 如何拆分一列但保留每个条目的键? - How do I split out a column but keep the key for each entry? 我在 Python 中使用 Pandas 并想知道如何拆分列中的值并在列中搜索该值 - I'm using Pandas in Python and wanted to know how to split a value in a column and search that value in the column 如何使用Python在搜索到的单词或字符上分割线并保留字符 - How do I split lines on a searched word or character using Python and keep the character 使用 Python,如何拆分多个分隔符并在输出列表中只保留一个? - Using Python, how do I split on multiple delimiters and keep only one in my output list? 我如何使用正则表达式python提取引号内的值? - how do i extract value inside quotes using regex python? 在Python中,如何拆分字符串并保留分隔符? - In Python, how do I split a string and keep the separators? 如何在python中使用正则表达式拆分并保留拆分词? - How to use split from regex in python and keep your split word? 如何使用 python 拆分 pandas 中的 dataframe 列值以获取另一列? - How do I split a dataframe column values in pandas to get another column using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM