'Join two or more DFs with same size

I have two data frames as shown below:

dataframe 1

dataframe 2

And input parameters CSV:

parameters csv

From the two DFs, I can generate this graph:

enter image description here

Code:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

df_1 = pd.read_csv("2DBM_50x50_Central_Aug21_Sim.cliped.csv")
df_2 = pd.read_csv("2DBM_50x50_Mauari_Aug21_Sim.cliped.csv")
df_all = pd.concat([df_1, df_2], axis=0)

variables = ["AAG", "DENS", "SRG", "RCG", "Thick"]
var_range = range(1, 101)

fig, axes = plt.subplots(2, 3)
flattened_axes = fig.axes
flattened_axes[-1].set_visible(False)

for i, var in enumerate(variables):
    var_columns = [f"TB_acc_{var}[{j:05}]" for j in var_range]
    data = df_all.melt(id_vars=["Period"], value_vars=var_columns, value_name=var)
    ax = flattened_axes[i]
    sns.boxplot(x="Period", y=var, width=0.2, linewidth =3, data=data, ax=ax, showfliers=False, color = "skyblue")
plt.tight_layout()
plt.show()

However, I needed to do it by integrating the code with parameters csv. So, I have:

Code

This code generates separated graphs for each line of the input CSV, and each one has a DF input:

graph df_1:

enter image description here

graph df_2:

enter image description here

My task, therefore, is to make the code integrated with the parameters CSV able to join both DFs in a single DF and then plot it into a single graph.


Any suggestion on how to do this?



Solution 1:[1]

You just have to read your parameters.csv file and filter the rows that are TRUE. Then, select the FilePath column from its output to read and concatenate the respective dataframes.

import pandas as pd

parameters = pd.read_csv('parameters.csv')
files_to_read = parameters[parameters['DoScenario']]['FilePath']
dfs_list = [pd.read_csv(f) for f in files_to_read]
df_concat = pd.concat(dfs_list, ignore_index=True)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 H4iku