我有一个具有5个分类值的df,我正在做以下操作,计算值的数量,然后绘制它们:
data['yr'].value_counts()
sns.countplot(x = 'yr', data = data)
data['season'].value_counts()
sns.countplot(x = 'season', data = data)
data['holiday'].value_counts()
sns.countplot(x = 'holiday', data = data)
data['workingday'].value_counts()
sns.countplot(x = 'workingday', data = data)
data['weathersit'].value_counts()
sns.countplot(x = 'weathersit', data = data)
在这种情况下,只有5列,因此该技术并不麻烦。但是,如果有许多这样的功能,那么编写这样的代码将非常麻烦。因此,我想知道如何修改上面的代码以使其更有效?
答案 0 :(得分:1)
您可以尝试这样:
columns = ["yr","season","holiday","workingday","weathersit"]
for column in columns:
data[column].value_counts()
sns.countplot(x = column, data = data)
答案 1 :(得分:1)
考虑到您使用熊猫加载了数据框,您可以使用这段代码,这还将为您提供
中所有列名称的列表。import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
data = pd.read_csv('cars.csv' , ';')
# Gets you the list of all columns in your dataframe.
columns = list(data.columns.values.tolist())
print(columns)
for column in columns:
data[column].value_counts()
sns.countplot(x = column, data = data)
plt.show()
plt.close()