mod*_*kur 2 python python-2.7 pandas
我正在尝试获取下面提到的数据帧中每个 zipCd 值的长度。当我运行下面的代码时,每条记录都会得到 958。我期待得到更像“4”的东西。有没有人看到问题是什么?
Code:
zipDfCopy['zipCd'].str.len()
Data:
print zipDfCopy[1:5]
Zip Code Place Name State State Abbreviation County \
1 544 Holtsville New York NY Suffolk
2 1001 Agawam Massachusetts MA Hampden
3 1002 Amherst Massachusetts MA Hampshire
4 1003 Amherst Massachusetts MA Hampshire
Latitude Longitude zipCd
1 40.8154 -73.0451 0 501\n1 544\n2 1001...
2 42.0702 -72.6227 0 501\n1 544\n2 1001...
3 42.3671 -72.4646 0 501\n1 544\n2 1001...
4 42.3919 -72.5248 0 501\n1 544\n2 1001...
Run Code Online (Sandbox Code Playgroud)
一种方法是转换为字符串并pd.Series.map与len内置一起使用。
pd.Series.str用于矢量化字符串函数,而pd.Series.astype用于更改列类型。
import pandas as pd
df = pd.DataFrame({'ZipCode': [341, 4624, 536, 123, 462, 4642]})
df['ZipLen'] = df['ZipCode'].astype(str).map(len)
# ZipCode ZipLen
# 0 341 3
# 1 4624 4
# 2 536 3
# 3 123 3
# 4 462 3
# 5 4642 4
Run Code Online (Sandbox Code Playgroud)
更明确的替代方法是使用np.log10:
df['ZipLen'] = np.floor(np.log10(df['ZipCode'].values)).astype(int) + 1
Run Code Online (Sandbox Code Playgroud)