我正在尝试使用Pandas拆分以下列:(df名称为count)
Location count
POINT (-118.05425 34.1341) 355
POINT (-118.244512 34.072581) 337
POINT (-118.265586 34.043271) 284
POINT (-118.360102 34.071338) 269
POINT (-118.40816 33.943626) 241
达到所需的结果:
X-Axis Y-Axis count
-118.05425 34.1341 355
-118.244512 34.072581 337
-118.265586 34.043271 284
-118.360102 34.071338 269
-118.40816 33.943626 241
我尝试删除单词“ POINT”和两个括号。但是,在专栏开头我遇到了一个额外的空白。我尝试使用:
count.columns = count.columns.str.lstrip()
但这并没有消除空白。
我希望使用此代码拆分列:
count = pd.DataFrame(count.Location.str.split(' ',1).tolist(),
columns = ['x-axis','y-axis'])
由于x轴和y轴之间的空间都可以用作分隔符,因此可以使用空白。
您可以使用.str.extract
具有捕获组的正则表达式模式:
df[['x-axis', 'y-axis']] = df.pop('Location').str.extract(r'\((\S+) (\S+)\)')
print(df)
count x-axis y-axis
0 355 -118.05425 34.1341
1 337 -118.244512 34.072581
2 284 -118.265586 34.043271
3 269 -118.360102 34.071338
4 241 -118.40816 33.943626
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句