我有一个数据框,其中包含各种字符串,我需要过滤,但不确定如何过滤。
locationcode
0 T4604760374N
1 T4604760374N
2 T4604760374N
3 T4604760374N
我只想在此列中显示第4到第7个字符-在熊猫中做到这一点的最佳方法是什么?
我通常会在SQL中使用子字符串。
谢谢!
答案 0 :(得分:3)
使用字符串切片
例如:
s = pd.Series(['T4604760374N', 'T4604760374N', 'T4604760374N', 'T4604760374N'])
print(s.str[4:7])
输出:
0 476
1 476
2 476
3 476
dtype: object
答案 1 :(得分:1)
您可以尝试以下方法:
df['locationcode'] = df['locationcode'].str[4:7]
print(df)
答案 2 :(得分:0)
您也可以使用str.slice
>>> s.str.slice(4,7)
0 476
1 476
2 476
3 476