Pandas 0.16.0 was released this week. Version 0.16.0 includes a new .assign()
method of DataFrames, which I'll use on some real world automobile MPG data below (data file).
In [1]:
%matplotlib inline
import pandas as pd
print pd.__version__
pd.options.display.mpl_style = 'default'
In [2]:
mpg_df = pd.read_csv('data/prius_gas.csv', na_values=['NA', 'DNF'])
mpg_df.head(3)
Out[2]:
The new .assign()
allows for more concise code in cases of "I want to plot a computed quantity." Here I plot how many miles were remaining in each fillup, assuming the MPG held steady at the average for that tank.
In [3]:
tank_capacity = 11.9
ax = (mpg_df.assign(miles_remaining=lambda x: (tank_capacity - x['Gallons']) * x['MPG'])
.plot(x='Date', y='miles_remaining', lw='2', legend=None))
ax.set_ylabel('Miles remaining');
Similar Posts
- Pandas Timedelta: histograms, unit conversion and overflow danger, Score: 0.982
- Pandas date parsing performance, Score: 0.979
- When joins go wrong, check data types, Score: 0.974
- Polar plots and shaded errors in matplotlib, Score: 0.958
- Analyzing 10 years of digital photography with python and pandas, Score: 0.942
Comments