[英]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.