pandasipythonswarmplot

How do I make a single swarm plot using two different data frames?


I have a dataframe that looks like this:

df1:

value 
1.2
2.2
3.3
4.3
3.1
2.1
....

I have another dataframe that looks similar but with different values:

df2:

value 
1.0
1.2
1.3
2.9
3.1
1.9
....

I want to make a swarm plot so that I have both df1 and df2 on the same plot, so I can directly compare the two. Ideally I would also like some sort of t-test between the two. Can I do this in pandas?


Solution

  • IIUC, you can do it this way by concatentating dataframes together and reshaping:

    df3 = pd.concat([df1,df2], axis=1, keys=['df1','df2']).stack(0)
    
    df3 = df3.reset_index(level=1)
    
    sns.swarmplot(x='level_1',y='value', data=df3)
    

    Output: enter image description here

    And T-Test:

    from scipy.stats import ttest_ind
    
    ttest_ind(df1['value'],df2['value'])
    

    Output:

    Ttest_indResult(statistic=1.3828776509907013, pvalue=0.1967994944161096)