对于if语句循环,要通过数据框架检查列值startswith一个特定值
我有一个带有地理杂质的shapefile。我不需要所有的行,只有某些行以ROUTE_ID为01、02和03开始的行。我使用for循环和if语句来尝试将我想要的数据附加到空数据框中。 示例数据:
route_id | from_measu | to_measure | street_pre | base_name |
---|---|---|---|---|
01006595050034-D | 5.799725 | 9.678965 | 215TH | |
D | 0 | 9.678965 | ST | 错误 |
0200006595050034 - | 是 | 以下 | 串联 | |
34-D | 0 | 9.678965 | 了 | 220 |
我的代码如下:
mnlrshwy = pd.DataFrame(columns=['ROUTE_ID','FROM_MEASU','TO_MEASURE','STREET_PRE',
'BASE_NAME'])
for x in mnlrs['ROUTE_ID']:
if x.startswith(('01','02','03')) is True:
mnlrshwy = x.append(mnlrs,ignore_index = True)
我得到 不明白为什么我会得到这样的东西。 任何建议都会有所帮助。
I have a shapefile that I am bringing in with geopandas. I do not need all the rows, just certain rows that start with 01, 02, and 03 for the route_id. I use a for loop and an if statement to try to append the data I want to an empty dataframe. Below is the sample Data:
ROUTE_ID | FROM_MEASU | TO_MEASURE | STREET_PRE | BASE_NAME |
---|---|---|---|---|
0100006595050034-D | 5.799725 | 9.678965 | 215th | |
0200006595050034-D | 0 | 9.678965 | ST | 220th |
0300006595050034-D | 5.799725 | 9.678965 | 215th | |
0400006595050034-D | 0 | 9.678965 | ST | 220th |
my code is as follows:
mnlrshwy = pd.DataFrame(columns=['ROUTE_ID','FROM_MEASU','TO_MEASURE','STREET_PRE',
'BASE_NAME'])
for x in mnlrs['ROUTE_ID']:
if x.startswith(('01','02','03')) is True:
mnlrshwy = x.append(mnlrs,ignore_index = True)
I get a concatenation error which I don't understand why I would get something like that.
Any suggestions would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用熊猫,则无需循环。
If you use pandas, you won't need a for loop.
在每个阶段执行选票,或将其转换为字符串,然后将其附加到您的数据框架上。
perform type check at each stage, or convert it into string then append it to your data frame.
上面的DF输出
是您的数据框架的数据。
输出
您在每次迭代时都会获得一个值。而且,如果空框架中没有索引,则不可能分配一个值。除非您在方括号中添加一个值。 ,您可以看到一个空的 Dataframe
在这里 在每个迭代中,所有列的行。左侧使用LOC是索引,右侧是列的名称。
df Output
Above is the data of your dataframe.
Output
You get one value at each iteration. And if there are no indexes in an empty dataframe, then it will not be possible to assign a value. Unless you add a value in square brackets. Here you can see about an empty dataframe
I filled in on each iteration the rows of all columns. Using loc on the left is the indexes, on the right is the name of the column.