Home > database >  Reading multiple DataFrames from a given input
Reading multiple DataFrames from a given input

Time:12-11

I have a couple of data frames given this way :

38 47  7 20 35
45 76 63 96 24
98 53  2 87 80
83 86 92 48  1
73 60 26 94  6

80 50 29 53 92
66 90 79 98 46
40 21 58 38 60
35 13 72 28  6
48 76 51 96 12

79 80 24 37 51
86 70  1 22 71
52 69 10 83 13
12 40  3  0 30
46 50 48 76  5

Could you please tell me how it is possible to add them to a list of dataframes?

Thanks a lot!

CodePudding user response:

First convert values to one DataFrame with separator misisng values (converted from blank lines):

df = pd.read_csv(file, header=None, skip_blank_lines=False)
    
print (df)
       0     1     2     3     4
0   38.0  47.0   7.0  20.0  35.0
1   45.0  76.0  63.0  96.0  24.0
2   98.0  53.0   2.0  87.0  80.0
3   83.0  86.0  92.0  48.0   1.0
4   73.0  60.0  26.0  94.0   6.0
5    NaN   NaN   NaN   NaN   NaN
6   80.0  50.0  29.0  53.0  92.0
7   66.0  90.0  79.0  98.0  46.0
8   40.0  21.0  58.0  38.0  60.0
9   35.0  13.0  72.0  28.0   6.0
10  48.0  76.0  51.0  96.0  12.0
11   NaN   NaN   NaN   NaN   NaN
12  79.0  80.0  24.0  37.0  51.0
13  86.0  70.0   1.0  22.0  71.0
14  52.0  69.0  10.0  83.0  13.0
15  12.0  40.0   3.0   0.0  30.0
16  46.0  50.0  48.0  76.0   5.0

And then in list comprehension create smaller DataFrames in list:

dfs = [g.iloc[1:].astype(int).reset_index(drop=True) 
       for _, g in df.groupby(df[0].isna().cumsum())]
print (dfs[1])
    0   1   2   3   4
0  80  50  29  53  92
1  66  90  79  98  46
2  40  21  58  38  60
3  35  13  72  28   6
4  48  76  51  96  12
  • Related