熊猫数据框列表的箱线图

时间:2020-08-10 03:41:51

标签: pandas boxplot

我有下一个数据帧列表

list = [df1,df2,df3,df4]

所有df都具有相同的结构

df = [col1,col2,col3]

我想在每个df中使用相同的列制作一个箱形图,但我不能,我尝试使用:

for df in dfs:
    df.boxplot(column='col1', subplots=True)

解决方案是:

new_df = pd.concat(list, axis=1)
for column in new_df:
    df.boxplot(['col1'])

2 个答案:

答案 0 :(得分:1)

您需要合并它。

df = pd.concat(lst)

然后绘图:

for column in df:
    plt.figure()
    df.boxplot(['col1'])

答案 1 :(得分:1)

您可以将所有dfs组合到一个更大的数据帧中(逐行连接),然后使用内置的boxplot方法创建所有列的箱形图,因为默认行为是为每个列创建一个箱形图。

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

np.random.seed(42)
df1 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])
df2 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])
df3 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])
df4 = pd.DataFrame(np.random.randint(0,100,size=(100, 3)), columns=['col1','col2','col3'])

dfs = [df1, df2, df3, df4]

## this will create a big dataframe composed of all the dfs
all_data = pd.concat(dfs, axis=1)

## this creates one boxplot for the first dataframe, loop through this
boxplot = all_data.iloc[:,0:3].boxplot()
plt.show()

enter image description here