[英]How do I split last 3 digits in values in a column in a Dataframe into two new Dataframes?
df=pd.DataFrame({'col1':[25021,25002,8002,40211,2232,""]})
col1
0 25021
1 25002
2 8002
3 40211
4 2232
5
I would like to get the following, not too sure how to split based on last 3 digits into col3 and whatever preceding into col1我想得到以下内容,不太确定如何根据最后 3 位数字拆分为 col3 以及前面的任何内容为 col1
col2 col3
0 25 021
1 25 002
2 8 002
3 40 211
4 2 232
5
This is my approach:这是我的方法:
df['col2'] = df['col1'].astype(str).str[-3:]
df['col1'] = df['col1'].astype(str).str[:-3]
Output: Output:
col1 col2
0 25 021
1 25 002
2 8 002
3 40 211
4 2 232
Try this.尝试这个。
df=pd.DataFrame({'col1':[25021,25002,8002,40211,2232]})
df['col2'] = df['col1'].astype(str).apply(lambda x:x[-3:]).astype(int)
df['col1'] = df['col1'].astype(str).apply(lambda x:x[:-3]).astype(int)
Just a play on Pandas' string split method;只是对 Pandas 的字符串拆分方法的一种尝试; you can wrap the delimiter(in a regex), so that it is included in the output:您可以包装分隔符(在正则表达式中),以便它包含在 output 中:
(df
.astype(str)
.col1
.str.split(r'(\d{3}$)', n=1, expand=True)
.drop(2,axis=1)
.set_axis(['col1','col2'],axis='columns')
)
col1 col2
0 25 021
1 25 002
2 8 002
3 40 211
4 2 232
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.