Question
Consider we have below Dataframe:
Two questions here:
* Question1
* Question2
HowTo
For Question1, the intuitive approach will look like:
A better approach is by using API pandas.cut:
Regarding the Question2, you can leverage another API pandas.pivot_table:
Two questions here:
* Question1
* Question2
HowTo
For Question1, the intuitive approach will look like:
- from collections import defaultdict
- data['hp_rank'] = data.apply(
- lambda r: 'Low (0-100)' if r.horsepower <= 100 else 'Medium (100-150)' if r.horsepower <= 150 else 'High (150+)',
- axis=1
- )
- bin_labels = ['Low (0-100)', 'Medium (100-150)', 'High (150+)']
- data['hp_rank'] = pd.cut(
- data['horsepower'],
- bins=[0, 100, 150, data['horsepower'].max()],
- labels=bin_labels
- )
- data['value'] = 1
- pd.pivot_table(data, values='value', columns=['drive-wheels'], index='hp_rank', fill_value=0, aggfunc=np.sum)
沒有留言:
張貼留言