【发布时间】:2021-05-05 11:09:31
【问题描述】:
我有一个示例数据框
data = {"col1" : ["1 first 1", "2 second 2", "third 3", "4 fourth 4"]}
df = pd.DataFrame(data)
print(df)
col1
0 1 first 1
1 2 second 2
2 third 3
3 4 fourth 4
我想提取列中的第一个digit 并删除它们
我试图提取使用
df["index"] = df["col1"].str.extract('(\d)')
col1 index
0 1 first 1 1
1 2 second 2 2
2 third 3 3
3 4 fourth 4 4
如果我使用replace,我想从col1 中删除提取的数字,开始和结束数字都将被替换。
期望的输出
col1 index
0 first 1 1
1 second 2 2
2 third 3 NaN
3 fourth 4 4
【问题讨论】:
标签: python pandas dataframe split extract