0

I am new to python and I was successful in using apply in a dataframe to create a new column inside a dataframe.

X['Geohash']=X[['Lat','Long']].apply (lambda column: geohash.encode(column[0],column[1],precision=8), axis=1)

this is calling the geohash function with the latitudes and longitudes per row and per column.

Now I have two new data frames one for Latitude and one for Longitude. Each dataframe has twenty columns and I want that the

.apply (lambda column: geohash.encode(column[0],column[1],precision=8), axis=1)

is called twenty times.

-First time the first dataframe-Latitude column with the first dataframe-Longitude column then

-the second time, the second dataframe-Latitude column with the second dataframe-Longitude column.

How I can do this iteration per column and at each iteration call the

.apply (lambda column: geohash.encode(column[0],column[1],precision=8), axis=1)

What I want to get is a new dataframe with twenty columns with each column being the result of the geohash function.

Ideas will be appreciated.

4
  • you have 2 datafames, each with a Latitude and Longtitude column? Commented May 14, 2019 at 7:48
  • @Alex P your requirements are confusing. If you mention your dummy input and expected output as well, it would be easy to answer. Commented May 14, 2019 at 8:05
  • I was thinking about it. That my code is not reproducible. Do you have any suggestions on how I can take big data frames and make them in a code that can create such data frame? Commented May 14, 2019 at 8:42
  • I have two data frames. One has only Latitudes and twenty columns. The other has only Longitudes and twenty columns. The twenty columns are just different time snapshots Commented May 14, 2019 at 8:43

1 Answer 1

1

You can do this by creating an "empty" dataframe with 20 columns, and then using df.columns[i] to loop through your other dataframes - something like this:

output = pd.DataFrame({i:[] for i in range(20)})

This creates an empty dataframe with all the columns you wanted (numbered).

Now, let's say longitude and latitude dataframes are called 'lon' and 'lat'. We need to join them into one dataframe Then:

lonlat = lat.join(lon)
for i in range(len(output.columns)):
    output[output.columns[i]] = lonlat.apply(lambda column: geohash.encode(column[lat.columns[i]], 
                                                            column[lon.columns[i]],
                                                            precision=8), axis=1)
Sign up to request clarification or add additional context in comments.

3 Comments

Great. I was able to edit your the code for mine and it works exactly as I needed. Thanks a lot. Really! Unfortunately I can not upvote you.
Glad I could help! Since you asked the question, you can accept my answer by clicking the star beneath the upvote/downvote arrows.
Yes! I did not know this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.