Python:如何只保留pandas列中的特定值?

ema*_*max 1 python string pandas

我有一个df如下的数据帧

df: 
    zip-code
0    00234
1    23450
2    23450
3    10786
4      0
5    xyzvd
Run Code Online (Sandbox Code Playgroud)

哪里type(df['zip-code']) = str.我想只保留5-digit值并删除所有其余的值.

Psi*_*dom 5

你可以使用str.match正则表达式:

df[df['zip-code'].str.match("^\d{5}$")]

#zip-code
#0  00234
#1  23450
#2  23450
#3  10786
Run Code Online (Sandbox Code Playgroud)