Austin T
Austin T

Reputation: 127

Plot Subset of Dataframe without Being Redundant

a bit of a Python newb here. As a beginner it's easy to learn different functions and methods from training classes but it's another thing to learn how to "best" code in Python.

I have a simple scenario where I'm looking to plot a portion of a dataframe spdf. I only want to plot instances where speed is greater than 0 and use datetime as my X-axis. The way I've managed to get the job done seems awfully redundant to me:

ts = pd.Series(spdf[spdf['speed']>0]['speed'].values, index=spdf[spdf['speed']>0]['datetime'])

ts.dropna().plot(title='SP1 over Time')

Is there a better way to plot this data without specifying the subset of my dataframe twice?

Upvotes: 1

Views: 80

Answers (1)

Jason
Jason

Reputation: 4546

You don't need to build a new Series. You can plot using your original df

df[df['col'] > 0]].plot()

In your case:

spdf[spdf['speed'] > 0].dropna().plot(title='SP1 over Time')

I'm not sure what your spdf object is or how it was created. If you'll often need to plot using the 'datetime' column you can set that to be the index of the df.If you're reading the data from a csv you can do this using the parse_dates keyword argument or it you already have the dfyou can change the index using df.set_index('datetime'). You can use df.info() to see what is currently being used at your index and its datatype.

Upvotes: 1

Related Questions