Reading multiple pandas dataframes from single plain text file

Viewed 95

I have an output file that contains 3 pandas data frames.

df1
          A         R         N         D
0  0.033333  0.086667  0.014444  0.058889
1  0.093333  0.084444  0.011111  0.034444
2  0.093333  0.084444  0.011111  0.034444


df2
          A         R         N         D
0  0.033333  0.008889  0.000000  0.055556
1  0.050000  0.016667  0.000000  0.058889
2  0.071111  0.018889  0.003333  0.027778


df3
          A         R         N         D
0  0.016667  0.007778  0.000000  0.003333
1  0.027778  0.002222  0.003333  0.024444
2  0.006667  0.000000  0.000000  0.055556

I need to use the df's in file as input for the next program.

I know I could just open the file and extract the respective lines and then put them into a df.

But I wonder if there is something built in that allows reading them by their name such as 'df1' or 'df2'.

I'm a bloody beginner - so be gentle

Thanks

1 Answers

The easiest way is to add a column to your dfs to indicate to which df those data belongs to and then concat them and save them:

df = pd.concat([df1.assign(df = 'df1'),df2.assign(df = 'df2'),df3.assign(df = 'df3')])
df.to_csv('data.csv')

After loading it again you can separate them

df = pd.read_csv('data.csv')
df1 = df[df['df'] == 'df1']
df2 = df[df['df'] == 'df2'] 
df3 = df[df['df'] == 'df3']
Related