如何在python中合并多个数据帧并在一个方框图中显示它们?
我使用的是一个二进制分类数据集,我正在尝试绘制所有样本的年龄,其中class==1的样本和class==0的样本? 我想知道如何合并firstDf、secondDf和thirdDf,并用python在一个方框图中显示它们如何在python中合并多个数据帧并在一个方框图中显示它们?,python,python-3.x,matplotlib,seaborn,data-science,Python,Python 3.x,Matplotlib,Seaborn,Data Science,我使用的是一个二进制分类数据集,我正在尝试绘制所有样本的年龄,其中class==1的样本和class==0的样本? 我想知道如何合并firstDf、secondDf和thirdDf,并用python在一个方框图中显示它们 age | class ------------ 1 | 1 2 | 1 3 | 0 4 | 1 5 | 0 6 | 1 7 | 1 8 | 0 9 | 0 10 | 1 import pandas as pd import matp
age | class
------------
1 | 1
2 | 1
3 | 0
4 | 1
5 | 0
6 | 1
7 | 1
8 | 0
9 | 0
10 | 1
import pandas as pd
import matplotlib.pyplot as plt
data = [['age', 'class'],
[1,1],
[2,1],
[3,0],
[4,1],
[5,0],
[6,1],
[7,1],
[8,0],
[9,0],
[10,1]]
firstDf = df['age']
secondDf = [df[df['class'] == 0]['age']]
thirdDf = [df[df['class'] == 1]['age']]
预期绘图
# subset dataframes
firstDf = df
secondDf = df[df['class'] == 0]
thirdDf = df[df['class'] == 1]
# combine dataframes and reset index
combined_df = pd.concat([firstDf, secondDf, thirdDf],
keys=['All', 'Class0', 'Class1']).reset_index(level=0)
# drop column 'class'
combined_df = combined_df.drop('class', axis=1)
# rename columns
combined_df.columns = ['category', 'age']
# fix datatype
combined_df['age'] = combined_df['age'].astype('int')
# import seaborn
import seaborn as sns
# plot boxplot
sns.boxplot(data=combined_df, x='category', y='age')