python-3.x 如何从Pandas字符串中提取前8个字符

hrirmatl  于 2023-02-06  发布在  Python
关注(0)|答案(4)|浏览(173)

我在一个 Dataframe 中有一个列,我正在尝试从一个字符串中提取8位数字。我该怎么做呢

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

我试了下面的代码,它不工作。

data['Order_Date'] = data['Shipment ID'][:8]
wmtdaxz3

wmtdaxz31#

您已接近,需要使用str进行索引,该索引适用于Serie s的每个值:

data['Order_Date'] = data['Shipment ID'].str[:8]

如果没有NaN s值,则为了获得更好的性能:
x一个一个一个一个x一个一个二个x
如果忽略str代码,则按位置过滤列,前N个值如下:

print (data['Shipment ID'][:2])
0    20180504-S-20000
1    20180514-S-20537
Name: Shipment ID, dtype: object
gopyfrb3

gopyfrb32#

您也可以使用str.extract

    • 例如:**
import pandas as pd

df = pd.DataFrame({'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']})
df["Order_Date"] = df["Shipment ID"].str.extract(r"(\d{8})")
print(df)
    • 输出:**
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
5cg8jx4n

5cg8jx4n3#

您还可以决定从-S删除到末尾

df["Order_Date"]=df['Shipment ID'].replace(regex=r"\-.*",value="")
df
        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

您还可以捕获前8位数字,然后删除所有内容,并使用捕获组的反向引用替换:

df['Shipment ID'].replace(regex=r"(\d{8}).*",value="\\1")
irlmq6kh

irlmq6kh4#

我决定使用带有子集的.str访问器:

data['p_dt'] = data['timestamp'].astype(str).str[:8]

相关问题