問題
みなさん、このコードをジオコード化するのに見つけました。これはすでに非常に役に立ち、別のデータフレームで完全に機能しました。残念ながら、別のcsvでスムーズに実行されていません。再び機能させるために何を変更できるかわかりません。私の場合、列に値がないため、問題はありますか? 誰かがそこに解決策を持っていますか、特に説明は大歓迎です:)! かなり前にthx。
ValueError: Columns must be same length as key
コード
locator = Nominatim(user_agent="YOUR_EMAIL")
geocode = RateLimiter(locator.geocode, min_delay_seconds=1)
df1['location'] = df1['geo_location'].apply(geocode)
df1['point'] = df1['location'].apply(lambda loc: tuple(loc.point) if loc else None)
df1[['latitude', 'longitude', 'altitude']] = pd.DataFrame(df1['point'].tolist(), index=df1.index)
2 行の CSV
,Unnamed: 0,Name,PLZ,Stadt,OT,Strasse,Telefon,Ansprechpartner,Ausbildungsberuf,E-Mail,Landkreis,Land,Bundesland,geo_location 0,0,Agrarerzeugergemeinschaft eG Pretzier,29410,Salzwedel,Pretzier,Königstedter Weg 4, 039037/95660,Frau Pieper,"Landwirt/in, TW FR Rinderhaltung",info@agrar-pretzier.de,Salzwedel,Deutschland,Sachsen-Anhalt,<geopy.extra.rate_limiter.RateLimiter object at 0x000001BA8719EBE0> 1,1,Drömlingshof GbR,38486,Klötze,Trippigleben,Dorfmitte 16 ,039004/512,"Gerhard Witten, Martina Herzog-Witten",Landwirt/in,g.witten@gmx.de,Salzwedel,Deutschland,Sachsen-Anhalt,<geopy.extra. 0x000001BA89965B20 の rate_limiter.RateLimiter オブジェクト>
トレースバック
Traceback (most recent call last):
File "FILEPATH", line 140, in <module>
df2[['latitude', 'longitude', 'altitude']] = pd.DataFrame(df2['point'].tolist(), index=df2.index)
File "C:\ProgramData\Anaconda3\envs\geocoding\lib\site-packages\pandas\core\frame.py", line 3597, in __setitem__
self._setitem_array(key, value)
File "C:\ProgramData\Anaconda3\envs\geocoding\lib\site-packages\pandas\core\frame.py", line 3634, in _setitem_array
check_key_length(self.columns, key, value)
File "C:\ProgramData\Anaconda3\envs\geocoding\lib\site-packages\pandas\core\indexers.py", line 428, in check_key_length
raise ValueError("Columns must be same length as key")
ValueError: Columns must be same length as key
check_key_length
def check_key_length(columns: Index, key, value: DataFrame):
"""
Checks if a key used as indexer has the same length as the columns it is
associated with.
Parameters
----------
columns : Index The columns of the DataFrame to index.
key : A list-like of keys to index with.
value : DataFrame The value to set for the keys.
Raises
------
ValueError: If the length of key is not equal to the number of columns in value
or if the number of columns referenced by key is not equal to number
of columns.
"""
if columns.is_unique:
if len(value.columns) != len(key):
raise ValueError("Columns must be same length as key")
else:
# Missing keys in columns are represented as -1
if len(columns.get_indexer_non_unique(key)[0]) != len(value.columns):
raise ValueError("Columns must be same length as key")