python - 值不在熊猫的范围内
问题描述
这是我的代码:
import pandas as pd
tmp_data=pd.read_csv("../data/geo/public.geo_commune.csv",sep=';')
#id;created;uuid;modified;name;geohash_file;gps_lat_file;gps_long_file;geohash_bing;gps_lat_bing;gps_long_bing;geohash_goog;gps_lat_goog;gps_long_goog;code_insee;code_cadastre;code_postal;departement;region;code_postal2
print(tmp_data[tmp_data['id'] == 6217])
communes = [
Commune(
id = tmp_data.loc[row]['id'],
uuid = tmp_data.loc[row]['uuid'],
name = tmp_data.loc[row]['name'],
geohash_file = tmp_data.loc[row]['geohash_file'],
gps_lat_file = tmp_data.loc[row]['gps_lat_file'],
gps_long_file = tmp_data.loc[row]['gps_long_file'],
code_insee = tmp_data.loc[row]['code_insee'],
code_postal = tmp_data.loc[row]['code_postal'],
code_postal2 = tmp_data.loc[row]['code_postal2'],
)
for row in tmp_data['id']
]
Commune.objects.bulk_create(communes)
我有这个错误:
回溯(最近一次通话):文件“/home/bussiere/.local/share/virtualenvs/GeoTransport-6Rxd3Krq/lib/python3.7/site-packages/pandas/core/indexes/range.py”,第 351 行,在 get_loc 返回 self._range.index(new_key) ValueError: 6217 is not in range
上述异常是以下异常的直接原因:
回溯(最后一次调用):文件“importPandasGeo.py”,第 34 行,在 tmp_data['id'] 中的行中 文件“importPandasGeo.py”,第 34 行,在 tmp_data['id'] 文件中的行中“ /home/bussiere/.local/share/virtualenvs/GeoTransport-6Rxd3Krq/lib/python3.7/site-packages/pandas/core/indexing.py”,第 895 行,在getitem 返回 self._getitem_axis(maybe_callable, axis=axis) 文件“/home/bussiere/.local/share/virtualenvs/GeoTransport-6Rxd3Krq/lib/python3.7/site-packages/pandas/core/indexing.py”,第 1124 行, 在 _getitem_axis 返回 self._get_label(key, axis=axis) 文件“/home/bussiere/.local/share/virtualenvs/GeoTransport-6Rxd3Krq/lib/python3.7/site-packages/pandas/core/indexing.py” ,第 1073 行,在 _get_label 返回 self.obj.xs(label, axis=axis) 文件“/home/bussiere/.local/share/virtualenvs/GeoTransport-6Rxd3Krq/lib/python3.7/site-packages/pandas/core /generic.py”,第 3739 行,在 xs loc = index.get_loc(key) 文件中“/home/bussiere/.local/share/virtualenvs/GeoTransport-6Rxd3Krq/lib/python3.7/site-packages/pandas/core /indexes/range.py", line 353, in get_loc raise KeyError(key) from err KeyError: 6217
但这条线运作良好:
print(tmp_data[tmp_data['id'] == 6217])
它给了我:
id created uuid ... departement region code_postal2
1795 6217 2020-01-08 17:20:08.091659+01 78DA163B1494401CBD6832AF048406FF257 ... NaN NaN NaN
[1 rows x 20 columns]
>head public.geo_commune.csv
id;created;uuid;modified;name;geohash_file;gps_lat_file;gps_long_file;geohash_bing;gps_lat_bing;gps_long_bing;geohash_goog;gps_lat_goog;gps_long_goog;code_insee;code_cadastre;code_postal;departement;region;code_postal2
3994;2020-01-08 07:56:04.095098+01;820B67C3D19D4C7DA4281E3DE4907113746;2020-01-12 12:12:53.151271+01;Argenvilliers;u08dw5bwzrdw;48.273752534979;0.967798168724;;;;;;;28010;;28480;;;
3842;2020-01-08 07:50:18.685592+01;1621BCFD0E9247499CC25AE834ED6A26957;2020-01-12 12:12:50.884786+01;Ardelu;u0975d2j33un;48.353115000000;1.911680371429;;;;;;;28009;;28700;;;
3844;2020-01-08 07:50:19.457564+01;390CA0A03C0B4AB1B14F8FB8BEB5F52E984;2020-01-12 12:12:50.892135+01;Mondonville Saint Jean;u0971s3k46v2;48.363891181818;1.825550795455;;;;;;;28257;;28700;;;
3845;2020-01-08 07:50:20.180927+01;0B34E895B5894E07A2965801DF7D7D24402;2020-01-12 12:12:50.895932+01;Saint Lubin De Cravant;u08v0g92bxy0;48.710802785714;1.089392690476;;;;;;;28346;;28270;;;
3846;2020-01-08 07:50:20.886304+01;0AB2686A6E944B2EB62B6152066C9B34264;2020-01-12 12:12:50.899976+01;Vierville;u0977847xg6t;48.384408230769;1.915047134615;;;;;;;28408;;28700;;;
3848;2020-01-08 07:50:22.424892+01;8076EF50310E4F0E82E6E8693CF2BDB7970;2020-01-12 12:12:50.908298+01;Allainville;u08vjmmyr11h;48.721324400000;1.293598661538;;;;;;;28003;;28500;;;
3849;2020-01-08 07:50:23.167292+01;A0B55E8630654823AA705332272D376E457;2020-01-12 12:12:50.912788+01;Barmainville;u096s2x71u1h;48.255236869565;1.954613173913;;;;;;;28025;;28310;;;
3850;2020-01-08 07:50:24.301801+01;29277930A4B84DAEB2958333D646061B353;2020-01-12 12:12:50.917579+01;Cernay;u08g5zuvztd6;48.383432805970;1.226330447761;;;;;;;28067;;28120;;;
3852;2020-01-08 07:50:25.98965+01;F9D27E5124CB49FDABFC4325686EB559383;2020-01-12 12:12:50.926628+01;Mereglise;u08fdyvur73c;48.289779263158;1.183747175439;;;;;;;28242;;28120;;;
解决方案
你可以通过行索引
communes = [
Commune(
id = tmp_data.loc[row]['id'].tolist(),
uuid = tmp_data.loc[row]['uuid'].tolist(),
name = tmp_data.loc[row]['name'].tolist(),
geohash_file = tmp_data.loc[row]['geohash_file'].tolist(),
gps_lat_file = tmp_data.loc[row]['gps_lat_file'].tolist(),
gps_long_file = tmp_data.loc[row]['gps_long_file'].tolist(),
code_insee = tmp_data.loc[row]['code_insee'].tolist(),
code_postal = tmp_data.loc[row]['code_postal'].tolist(),
code_postal2 = tmp_data.loc[row]['code_postal2'].tolist(),
)
for row in range(tmp_data.shape[0])
]
或者你可以通过 id
communes = [
Commune(
id = tmp_data.loc[tmp_data["id"]==i]['id'].iloc[0],
uuid = tmp_data.loc[tmp_data["id"]==i]['uuid'].iloc[0],
name = tmp_data.loc[tmp_data["id"]==i]['name'].iloc[0],
geohash_file = tmp_data.loc[tmp_data["id"]==i]['geohash_file'].iloc[0],
gps_lat_file = tmp_data.loc[tmp_data["id"]==i]['gps_lat_file'].iloc[0],
gps_long_file = tmp_data.loc[tmp_data["id"]==i]['gps_long_file'].iloc[0],
code_insee = tmp_data.loc[tmp_data["id"]==i]['code_insee'].iloc[0],
code_postal = tmp_data.loc[tmp_data["id"]==i]['code_postal'].iloc[0],
code_postal2 = tmp_data.loc[tmp_data["id"]==i]['code_postal2'].iloc[0],
)
for i in tmp_data["id"]
]
推荐阅读
- machine-learning - 使用神经网络的多元回归(多个目标变量)是一项微不足道的任务吗?
- shopify - 我如何使用应用代理来使用 react 编写我的 shopify 应用程序来登录客户
- android - 如何忽略我不想在 Android 视图绑定库中创建绑定的视图组件?
- c - 如何给结构中的字符指针赋值并以队列的形式存储
- python - 搜索具有许多不同值的许多行
- mysql - 错误代码:1066。不是唯一的表/别名:'ordertbl' - 无法执行内部连接 MYSQL
- mysql - How to import MongoDb collection data to MySQL?
- c# - 在这种情况下如何避免重复代码?
- android - How to create aab via air33?
- java - 如何将对象从`flatMap`传递给链中的`subscribe`?