Home > Mobile >  How to draw a boxplot with only chosen columns using matplotlib?
How to draw a boxplot with only chosen columns using matplotlib?

Time:11-19

I have an iris dataset that has 5 columns,4 of which are useful features and I wanna draw a boxplot using them,but I also have a useless column that I wanna drop out,how can I do it? columns The code looks like this:

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

iris_data = load_iris()
# print(iris_data)
iris = pd.DataFrame(data=np.c_[iris_data['data'], iris_data['target']],
                    columns=iris_data['feature_names']   ['species'])
setosa = iris.head(50)
print(setosa.columns)
plt.boxplot(setosa, vert=True)

plt.show()

I expect a boxplot only has the first four features

CodePudding user response:

The column can be dropped using DataFrame.drop(columns='<your_column_name>'). More information can be found in the documentation of pandas

import pandas as pd
import numpy as np
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt

iris_data = load_iris()

iris = pd.DataFrame(data=np.c_[iris_data['data'], iris_data['target']],
                    columns=iris_data['feature_names']   ['species'])
iris = iris.drop(columns="species")
setosa = iris.head(50)
print(setosa.columns)
plt.boxplot(setosa, vert=True)

plt.show()
  • Related