'How to loop through pandas data frame to make boxplots at once

I have a data frame like this with thousand of entries and I want to make box plot to check the outliers in my data.

HR O2Sat Temp SBP DBP Resp
110.9 102.5 57.21 165.2 64.0 15.2
97.0 95.0 38.72 98.0 72.0 19.0
89.0 99.0 45.02 112.0 62.5 22.0
90.0 95.0 36.7 175.0 105.0 30.0
103.0 88.5 37.47 122.0 104.0 24.5

I am using seaborn library to make Boxplots. But I have to write 6 different code lines for each column like this:

import seaborn as sns 
sns.boxplot(y = 'HR', data = box_df_1) 
sns.boxplot(y = 'O2Sat', data = box_df_1) 
sns.boxplot(y = 'Temp', data = box_df_1) 
sns.boxplot(y = 'SBP', data = box_df_1) 
sns.boxplot(y = 'DBP', data = box_df_1) 
sns.boxplot(y = 'Resp', data = box_df_1) 

Can someone help me with some code in which Loop is used and a loop will make the boxplots at once using seaborn, and I don't have to write separate line of code for each column?

Regards, Huzaifa



Solution 1:[1]

create a list for the columns:

cols = ['HR', 'O2Sat', 'Temp', 'SBP', 'DBP', 'Resp']

iterate over the list

for item in cols:     
    sns.boxplot(x = box_df_1[item])
    plt.show()

Solution 2:[2]

You can try this,

protected void Button1_Click(object sender, EventArgs e)
    {
        string cs = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
        SqlConnection cn = new SqlConnection(cs);
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "insert into sample values(@payment)";
        cmd.Parameters.Clear();
        cmd.Parameters.AddWithValue("@payment", payment.SelectedValue);
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        lblmsg.Text = "Data entered successfully!!!";
    }

Solution 3:[3]

I found the answer to my own question if someone needs it

try
{
    var connection = getConnection();
    connection.Open();

    if (CashRadio.Checked == true)
    {      
        var command = new SqlCommand
        {
            Connection = connection,
            CommandText ="INSERT INTO type_payment(cash,card) values(1,0)"
        };          
        command.Parameters.Clear();

        int result = command.ExecuteNonQuery();
        if (result > 0)
        {
            MessageBox.Show("Succsefully picked type");
        }
        else
        {
            MessageBox.Show("error");
        }
    }
    else if (CardRadio.Checked == true)
    {
        var command = new SqlCommand
        {
            Connection = connection,
            CommandText = "INSERT INTO type_payment(cash,card) values(0,1)"
        };
        command.Parameters.Clear();

        int result = command.ExecuteNonQuery();
        if (result > 0)
        {
            MessageBox.Show("Succesfully picked type");
        }
        else
        {
            MessageBox.Show("Error");
        }
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Solution 4:[4]

Luka,

Try it below. Add HTML Markup like:

<asp:CheckBox ID="chkCash" runat="server" />

.cs file add below code.

string Cash = chkCash.Checked ? "Y" : "N";

And send or add value like.

SqlCommand cmd = new SqlCommand("INSERT INTO Employees(Cash) VALUES(@Cash)"))
    {           
        cmd.Parameters.AddWithValue("@Cash", Cash);
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emi OB
Solution 2 lakshna S
Solution 3 Lukasz Szczygielek
Solution 4 Frankich