创建一个函数,该函数将接受 DataFrame 作为输入并返回所有适当分类特征的饼图
我可以使用“Churn”列创建 1 个饼图来对数据进行分组,但是,不确定如何创建一个函数来接受 DataFrame 作为输入并返回所有适当的分类特征和饼图。在饼图中显示百分比分布?
作为 DataFrame,我使用“电信客户流失.csv"
f,axes=plt.subplots(1,2,figsize=(17,7))
df_churn['Churn'].value_counts().plot.pie(autopct='%1.1f%%',ax=axes[0])
sns.countplot('Churn',data=df_churn,ax=axes[1])
axes[0].set_title('Categorical Variable Pie Chart')
plt.show()
I can create 1 pie-chart using the 'Churn' column to group the data, however, not sure how to create a function that will accept a DataFrame as input and return pie-charts for all the appropriate Categorical features & show percentage distribution in the pie charts?
As DataFrame, I am using "Telco-Customer-Churn.csv"
f,axes=plt.subplots(1,2,figsize=(17,7))
df_churn['Churn'].value_counts().plot.pie(autopct='%1.1f%%',ax=axes[0])
sns.countplot('Churn',data=df_churn,ax=axes[1])
axes[0].set_title('Categorical Variable Pie Chart')
plt.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了这样的事情,不确定我是否做对了:-
#%% PlotMultiplePie
输入:df = Pandas dataframe,categorical_features = 功能列表,dropna = 使用 NaN 的布尔变量
输出:打印多个 px.pie()
def PlotMultiplePie(df_churn,categorical_features = None,dropna = False):
# 设置 30 个唯一变量的阈值,超过 50 个会导致饼图难看
阈值 = 40
I did something like this, not sure if i did it right:-
#%% PlotMultiplePie
Input: df = Pandas dataframe, categorical_features = list of features , dropna = boolean variable to use NaN or not
Output: prints multiple px.pie()
def PlotMultiplePie(df_churn,categorical_features = None,dropna = False):
# set a threshold of 30 unique variables, more than 50 can lead to ugly pie charts
threshold = 40
这对我有用。定义了一个函数来绘制
dataframe
中所有分类变量的饼图。This worked for me. Defined a function to plot the pie charts, for all categorical variables in a
dataframe
.