如何从 pandas 字符串中提取前8个字符 [英] How to extract first 8 characters from a string in pandas
本文介绍了如何从 pandas 字符串中提取前8个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在数据框中有一个列,我正在尝试从字符串中提取8位数字.我该怎么办
I have column in a dataframe and i am trying to extract 8 digits from a string. How can I do it
Input
Shipment ID
20180504-S-20000
20180514-S-20537
20180514-S-20541
20180514-S-20644
20180514-S-20644
20180516-S-20009
20180516-S-20009
20180516-S-20009
20180516-S-20009
预期产量
Order_Date
20180504
20180514
20180514
20180514
20180514
20180516
20180516
20180516
20180516
我尝试了下面的代码,但没有用.
I tried below code and it didnt work.
data['Order_Date'] = data['Shipment ID'][:8]
推荐答案
您很接近,需要使用str
进行索引,该索引适用于Serie
s的每个值:
You are close, need indexing with str
which is apply for each value of Serie
s:
data['Order_Date'] = data['Shipment ID'].str[:8]
如果没有NaN
的值,则为获得更好的性能:
For better performance if no NaN
s values:
data['Order_Date'] = [x[:8] for x in data['Shipment ID']]
print (data)
Shipment ID Order_Date
0 20180504-S-20000 20180504
1 20180514-S-20537 20180514
2 20180514-S-20541 20180514
3 20180514-S-20644 20180514
4 20180514-S-20644 20180514
5 20180516-S-20009 20180516
6 20180516-S-20009 20180516
7 20180516-S-20009 20180516
8 20180516-S-20009 20180516
如果按位置省略str
代码过滤器列,则前N个值如下:
If omit str
code filter column by position, first N values like:
print (data['Shipment ID'][:2])
0 20180504-S-20000
1 20180514-S-20537
Name: Shipment ID, dtype: object
这篇关于如何从 pandas 字符串中提取前8个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文