Skip to content
Advertisement

Using one column values as index to list type values in another in pandas

We have data representing temperature forecast for every 3 hours period from the moment. We also know number of 3 hour periods after which the weather is needed. So, we have dataframe:

JavaScript

We need to create column with particular element from the list in ‘T_forecast’ columns.The result should be:

JavaScript

I can get it for particular value, with code:

JavaScript

But I struggle creating column out:

JavaScript

Using the for loop is not an option since the original dataframe is very large and the server will choke. What can lead to solving the issue?

Advertisement

Answer

Loop solution

JavaScript

Non-loop solution

** lists should have same length across all rows

** This solution will perform around 2x better only on large data sets >= 500K

JavaScript

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