如何在Python中使用Plots执行univaraite分析

2024-10-03 09:11:21 发布

您现在位置:Python中文网/ 问答频道 /正文

下面有一个数据集,我想对IncomeCategory执行单变量分析,如图所示。这里的点在数字范畴中1被视为Male,而{}被视为{}。
有什么办法可以解决这个问题吗。在

Income  Population  Number  Category
54        77           1       A
23        88           1       A
44        87           0       B
55        88           0       B
66        89           1       B
73        90           0       A
12        89           1       C
34        9            0       C
54        77           1       A
23        88           1       A
44        87           0       B
55        88           0       B
66        89           1       B
73        90           0       A
12        89           1       C
34        9            0       C

enter image description here


Tags: 数据number数字malepopulationcategory办法income
2条回答

我不确定你的问题是否清楚。但是,以下图表通常用于进行单变量和双变量分析。在

import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame({'Income': [54,23,44,55,66,],
                   'Population':[77,88,87,88,89],
                   'Number':[1,1,0,0],
                   'Category':['A','A','B','B','C']})

### Univariate analysis
sns.distplot(df.Income) # numeric
sns.boxplot(df.Income) # numeric
sns.distplot(df.Population)
sns.countplot(df.Category) # categorical
sns.countplot(df.Number)

## Bivariate analysis
sns.jointplot('Income', 'Population', data = df, kind='scatter')
sns.lmplot(df.Income, df.Population, data=df, hue='Number', fit_reg=False)
sns.countplot(Category, hue = 'Number', data=df)

## Multivariate analysis
sns.pairplot(df.select_dtypes(include=[np.int, np.float]])

如果将数据放入熊猫数据框中,则可以轻松地将雄性和雌性的值分开,例如(只需使用IncomeNumber):

import pandas as pd
# a dictionary of the data
data = {'Income': [54, 23, 44, 55, 66, 73, 12], 'Number': [1, 1, 0, 0, 1, 0, 1]}
# put the data into a pandas DataFrame
d = pd.DataFrame(data=data)

# get a list of Income for the Males
incomem = d['Income'][d['Number'] == 1].tolist() # you don't really need the tolist() call

# get a list of Income for the Females
incomef = d['Income'][d['Number'] == 0].tolist()

然后您可以使用例如示例here来绘制条形图。这个plot.ly公司包对于这类事情也很好,如示例here。在

相关问题 更多 >