Skip to content
Advertisement

Convert Array to dataframe with Longitude, Lattitude coordinates

Imported Libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

I am trying to creat a Heatmap out of my strava dataset ( which turns to be a csv file of 155479 rows with Georaphical cooridnates) I tried first to display the whole dataset on Folium using python, the problem is that Folium seemed to crash when i tried to upload the whole dataset ( it was working with a sample). Meanwhile I found this post https://towardsdatascience.com/create-a-heatmap-from-the-logs-of-your-activity-tracker-c9fc7ace1657 the code is working in displaying all the datset.

size_x, size_y = 1000, 1000
​
df2 = df[(df.lat > LAT_MIN) & (df.lat < LAT_MAX) & 
         (df.lon > LAT_MIN) & (df.lon < LAT_MAX)].copy()
df2['x'] = (size_x * (df2.lon - df2.lon.min())/(df2.lon.max() -df2.lon.min())).astype(int)
df2['y'] = (size_y * (df2.lat - df2.lat.min())/(df2.lat.max() - df2.lat.min())).astype(int)


data = np.zeros((size_x,size_y)) 
width = 2 ​ 
df3 = df2[['x', 'y','type']].groupby(['x', 'y']).count().reset_index() 
for index, row in df3.iterrows():
     x = int(row['x'])
     y = int(row['y'])
     data[y - width:y + width, x - width:x + width] += row ['type'] ​ 
 max = len(df2.source.unique()) * 1

and creating a descent heatmap

 #data[data > max] = max   data = (data - data.min()) / (data.max() -
 #data.min()) cmap = plt.get_cmap('hot') 
 #data = cmap(data)

However when i try to convert this below array to a Dataframe

df_data = pd.DataFrame(data) df_data.head()

​ ​I dont understand the below error

ValueError: Must pass 2-d input. shape=(1000, 1000, 4)

Advertisement

Answer

The error means that Pandas can’t organize your data into a table. By definition, tables have 2 dimensions (rows and columns), but the data you passed has 3 dimensions: 1000, 1000 and 4.

To make it work, you should reshape the data to 2 dimensions.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement