Given the CSV data:
,fan1,fan2,foil1,foil2 0,0.0,0.0,0.0,0.125 1,0.0625,0.0,0.0625,0.125 2,0.0625,0.0,0.0,0.3125
Which I want to turn into a kind of annotated pivot-table which can be plotted as a bar-plot:
,Err,PairType,StimType 0,0.0,Target,1 1,0.0625,Target,1 2,0.0625,Target,1 0,0.0,Target,2 1,0.0,Target,2 2,0.0,Target,2 0,0.0,RPFoil,1 1,0.0625,RPFoil,1 2,0.0,RPFoil,1 0,0.125,RPFoil,2 1,0.125,RPFoil,2 2,0.3125,RPFoil,2
I currently accomplish this with the following code:
import numpy as np import pandas as pd def df_plotable(model_err: pd.DataFrame): t_len = len(model_err.fan1) cols = ("Err", "PairType", "StimType") fan1_df = pd.DataFrame(np.array([model_err.fan1, ["Fan"]*t_len, [1]*t_len]).T, columns=cols) fan2_df = pd.DataFrame(np.array([model_err.fan2, ["Fan"]*t_len, [2]*t_len]).T, columns=cols) foil1_df = pd.DataFrame(np.array([model_err.foil1, ["Foil"]*t_len, [1]*t_len]).T, columns=cols) foil2_df = pd.DataFrame(np.array([model_err.foil2, ["Foil"]*t_len, [2]*t_len]).T, columns=cols) new_model_err = pd.concat((fan1_df, fan2_df, foil1_df, foil2_df)) new_model_err["Err"] = new_model_err["Err"].astype(float) new_model_err["StimType"] = new_model_err["StimType"].astype(int) return new_model_err
Such that:
df = pd.read_csv("in.csv", "r", delimiter=",", index_col=0) df_plotable(df).to_csv("out.csv")
Is there a way to do this more cleanly?