Python Forum
Manipulating panda dataframes more python-like
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulating panda dataframes more python-like
#3
(Jan-20-2023, 12:57 AM)badtwistoffate Wrote: Unfortunately, I don't think I'm doing it in a python-efficient way as the script has been executing for over three hours. Here's some example code. Is there a better way to compare time stamps and not rely as heavily on for loops?
Pandas often need a tolal diffent approch than how stuff are done in Python,the loops are usually out 🚀
Look at this blog post.
So try to work on whole dataframs(build in tools) using vectorization(this mean avoid Python-level loops)
To give exampe how to compare time between two or more dataframs.
import pandas as pd

# Create Dataframe 1
date1 = ['2022-01-01 00:00:00', '2022-01-02 12:00:00', '2022-01-03 18:00:00']
wind_speed1 = [5, 6, 7]
df1 = pd.DataFrame({'date_time': pd.to_datetime(date1), 'wind_speed': wind_speed1})

# Create Dataframe 2
date2 = ['2022-01-02 08:00:00', '2022-01-03 15:00:00', '2022-01-04 20:00:00']
wind_speed2 = [8, 9, 10]
df2 = pd.DataFrame({'date_time': pd.to_datetime(date2), 'wind_speed': wind_speed2})

# Convert the specific time to a 'pandas.Timestamp' object
specific_time = df1.iloc[1]['date_time'].time()
specific_time = pd.to_datetime(specific_time, format='%H:%M:%S').time()

# Compare time less than a specific time and show the result
print(df2[df2['date_time'].dt.time < specific_time])
Output:
date_time wind_speed 0 2022-01-02 08:00:00 8
Reply


Messages In This Thread
RE: Manipulating panda dataframes more python-like - by snippsat - Jan-27-2023, 02:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Panda Exercise nyzs92 0 1,510 Sep-24-2021, 03:35 AM
Last Post: nyzs92
  Manipulating code to draw a tree Py_thon 8 3,258 Nov-21-2019, 05:00 PM
Last Post: sumana
  Manipulating List frenchyinspace 2 2,700 Oct-08-2019, 07:57 AM
Last Post: perfringo
  Manipulating __init__ method schniefen 5 3,532 May-06-2019, 11:22 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020