This should be a simple question, but hours of searching and reading the docs has not helped find an answer to this simple, yet age-old pandas "how to do something if the value in one column is < the value in another column.
My project is to graph a series of time cycles so I can visually see when in the calendar year the cycles start and end. The time period start and end will be different for each period.
The x-axis is the day of the year, left boundary is 0 (1 Jan), and the right boundary is 365 (31 Dec). The y-axis is the time cycle name.
If the cycle start and cycle end are in the same calendar year, then I simply draw a horizontal line between them using the following code:
ax.hlines(df['Cycle Name'], xmin=df['Cycle Start'], xmax=df['Cycle End'])
If either the cycle start in the previous calendar year, or the cycle ends in the following calendar year, I draw 2 lines to connect the cycle start/end to the graph boundaries using the following code:
ax.hlines(df['Cycle Name'], xmin=0, xmax=df['Cycle Start'])
ax.hlines(df['Cycle Name'], xmin=df['Cycle End'], xmax=365)
What I can not figure out how to do is the simple pandas conditional magic required to do the following;
if df['Cycle Start'] < df['Cycle End']:
'draw one line as shown above'
else:
'draw 2 lines as shown above'
What is the pandas syntax to do something based on the value of one column being compared to the value of another column in the same row?
(note I am not modifying the dataframe in anyway, just doing something externally based on the result of the comparison of 2 values in the dataframe in each row)
Aucun commentaire:
Enregistrer un commentaire