Cara menggunakan max axis 1 python

Often when visualizing data using a bar chart, you’ll have to make a decision about the orientation of your bars. While there are no concrete rules, there are quite a few factors that can go into making this decision. For example, when grouping your data by an ordinal variable, you may want to display those groupings along the x-axis. On the other hand, when grouping your data by a nominal variable, or a variable that has long labels, you may want to display those groupings horizontally to aid in readability.

This recipe will show you how to go about creating a horizontal bar chart using Python. Specifically, you’ll be using pandas plot() method, which is simply a wrapper for the matplotlib pyplot API.

In our example, you'll be using the publicly available San Francisco bike share trip dataset to identify the top 15 bike stations with the highest average trip durations. You will then visualize these average trip durations using a horizontal bar chart. The steps in this recipe are divided into the following sections:

You can find implementations of all of the steps outlined below in this example Mode report. Let’s get started.

Data Wrangling

You’ll use SQL to wrangle the data you’ll need for our analysis. For this example, you’ll be using the sf_bike_share_trips dataset available in Mode's Public Data Warehouse. Using the within the , make sure your data source is set to the Mode Public Warehouse data source and run the following query to wrangle your data:

select *
from modeanalytics.sf_bike_share_trips

Once the SQL query has completed running, rename your SQL query to SF Bike Share Trip Rankings so that you can easily identify it within the Python notebook:

Cara menggunakan max axis 1 python

Data Analysis

Now that you have your data wrangled, you’re ready to move over to the Python notebook to prepare your data for visualization. Inside of the Python notebook, start by importing the Python modules that you'll be using throughout the remainder of this recipe:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter

Mode pipes the results of your SQL queries into a pandas assigned to the variable datasets. You can use the following line of Python to access the results of your SQL query as a dataframe and assign them to a new variable:

df = datasets['SF Bike Share Trip Data']

As previously mentioned, your goal is to visualize the 15 start stations with the highest average trip duration. You can analyze the dataframe to find these stations using the following method chain on our existing dataframe object:

x = df.groupby('start_station_name')['duration'].mean().sort_values().tail(15)

We now have a new dataframe assigned to the variable

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
0 that contains the top 15 start stations with the highest average trip durations. Now that we have our dataset aggregated, we are ready to visualize the data.

Data Visualization

To create a horizontal bar chart, we will use pandas plot() method. We can specify that we would like a horizontal bar chart by passing

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
2 to the
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import StrMethodFormatter
3 argument:

x.plot(kind=‘barh’)

Pandas returns the following horizontal bar chart using the default settings:

Cara menggunakan max axis 1 python

You can use a bit of matplotlib styling functionality to further customize and clean up the appearance of your visualization:

Learn the fundamentals of neural networks and how to build deep learning models using Keras 2.0 in Python.

See DetailsRight Arrow

Start Course

Introduction to Natural Language Processing in Python

Beginner

4 hr

94.4K

Learn fundamental natural language processing techniques using Python and how to apply them to extract insights from real-world text data.

If you want the index of the maximum, use idxmax. This is the equivalent of the numpy.ndarray method argmax.

Parametersaxis{index (0), columns (1)}

Axis for the function to be applied on. For Series this parameter is unused and defaults to 0.

skipnabool, default True

Exclude NA/null values when computing the result.

levelint or level name, default None

If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.

Deprecated since version 1.3.0: The level keyword is deprecated. Use groupby instead.

numeric_onlybool, default None

Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.