Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stack data frames on top of each other in Pandas

I have a dataframe with 96 columns:

df.to_csv('result.csv')

out (excel):

    Run 1   Run 2   Run 3   Run 4   Run 5   Run 6   Run 7   Run 8   Run 9   Run 10  Run 11  Run 12  Run 13  Run 14  Run 15  Run 16  Run 17  Run 18  Run 19  Run 20  Run 21  Run 22  Run 23  Run 24  Run 25  Run 26  Run 27  Run 28  Run 29  Run 30  Run 31  Run 32  Run 33  Run 34  Run 35  Run 36  Run 37  Run 38  Run 39  Run 40  Run 41  Run 42  Run 43  Run 44  Run 45  Run 46  Run 47  Run 48  Run 49  Run 50  Run 51  Run 52  Run 53  Run 54  Run 55  Run 56  Run 57  Run 58  Run 59  Run 60  Run 61  Run 62  Run 63  Run 64  Run 65  Run 66  Run 67  Run 68  Run 69  Run 70  Run 71  Run 72  Run 73  Run 74  Run 75  Run 76  Run 77  Run 78  Run 79  Run 80  Run 81  Run 82  Run 83  Run 84  Run 85  Run 86  Run 87  Run 88  Run 89  Run 90  Run 91  Run 92  Run 93  Run 94  Run 95  Run 96
12  5194322.07  5195697.94  5195730.25  5196009.11  5195054.02  5193386.44  5192664.99  5193381.71  5193652.29  5193637.02  5191110.57  5190267.47  5190739.45  5190416.85  5189592.97  5188898.89  5188461.01  5188735.44  5189156.83  5188870.35  5188306.12  5187746.51  5188023.45  5187536.91  5188085.85  5188634.95  5187861.42  5188124.97  5187076.75  5189218.62  5189052.51  5188571.63  5188486.76  5188502.68  5188318.63  5188512.5   5188409.83  5188250.86  5188885.18  5188999.83  5189365.09  5190159.72  5189771.1   5190136.3   5191179.72  5191256.35  5191147.97  5191712.32  5192430.88  5193407.95  5192603.89  5192248.7   5192197.65  5193096.79  5193005.25  5193985.8   5193451.22  5193489.16  5193562.72  5194621.43  5194170.84  5194198.19  5194866.16  5194030.81  5194421.67  5193745.31  5195458.37  5196342.62  5194881.29  5195036.46  5193627.87  5194470.9   5195017.44  5194402.87  5194659.24  5194751.51  5195016.87  5194802.11  5195467.68  5194654.04  5195622.23  5194709.45  5195050.77  5195097.58  5195987.22  5195831.3   5194776.48  5193605.12  5194317.87  5194089.21  5194563.64  5193895.14  5194140.95  5193791.85  5193915.21  5194343.34
13  1453304.43  1454792.33  1454807.96  1454768.09  1455077.49  1454644.59  1454545.94  1454930.93  1455214.85  1455342.12  1455188.92  1454972.08  1455358.05  1455533.45  1455208.56  1454913.89  1455124.45  1454644.83  1455071.25  1454812.46  1454838.9   1454842.33  1454895.52  1454838.55  1454888.25  1455024.08  1454624.57  1455159.29  1454889.65  1454906.92  1454789.36  1454579.06  1455060.12  1455108.26  1455289.8   1455269.54  1455227.93  1455734.55  1455846 1455774.16  1456130.24  1456289.94  1455711.1   1456447.68  1456588.78  1456796.61  1456867.04  1457081.75  1457274.68  1457155.16  1457782.73  1457065.11  1457459.15  1457347.08  1457837.54  1457999.87  1458171.82  1458241.76  1458320.08  1458622.22  1458574.79  1458586.67  1458701.91  1458749.17  1458869.01  1458755.66  1458885 1459167.12  1458881.12  1459110.4   1458918 1459297.49  1459375.28  1459338.09  1459413.22  1459726.96  1459926.75  1459943.81  1460193.37  1460242.02  1460274.7   1460319.25  1460494.5   1460347.8   1460589.02  1460436.82  1460754.06  1460643.79  1460803.29  1460817.97  1460948.1   1460903.97  1460944.45  1460874.04  1460929.33  1461072.84
14  193379.75   193027.34   192806.25   192501.2    192602.7    192477.86   192402.72   192408.76   192421.74   192400.59   192345.37   192312.98   192331.79   192357.29   192277.84   192270.06   192232.67   192170.09   192216.06   192182.3    192163.13   192145.32   192164.63   192157.59   192134.08   192172.82   192098.36   192146.81   192106.65   192082.12   192057.73   192065.45   192080.46   192128.27   192096.82   192120.97   192081.97   192166.45   192157.38   192121.78   192203.97   192215.73   192098.89   192181.45   192211.12   192234.93   192245.5    192282.35   192290.05   192278.03   192370.19   192250.39   192308.68   192264.65   192339.55   192365.62   192394.12   192385.72   192403.42   192431.52   192408.2    192414.77   192419.74   192424.98   192432.85   192422.79   192444.94   192454.58   192456.89   192449  192451.98   192507.83   192490.77   192504.55   192520.85   192539.33   192549.03   192578.96   192618.21   192638.83   192629.15   192617.57   192651.62   192626.81   192649.6    192636.68   192703.22   192661.42   192687.33   192704.48   192729.77   192731.6    192742.22   192701.82   192729.55   192743.99
15  157553.12   157252.95   157080.77   156941.24   156887.86   156776.95   156669.69   156664.82   156695.03   156652.3    156653.55   156576.01   156586.19   156620.33   156558.26   156539  156501.76   156445.28   156465.98   156435.93   156436.62   156444.92   156422.48   156446.25   156426.4    156447.14   156397.15   156421.85   156391.94   156370.94   156337.46   156364.73   156380.6    156399.43   156389.75   156386.21   156346.02   156453.62   156442.94   156392.71   156436.89   156449.56   156363.24   156443.5    156448.72   156429.21   156479.57   156498  156535.45   156528.7    156603.81   156486.44   156524.97   156482.7    156558.25   156574.41   156585.45   156572.05   156610.77   156638.11   156607.3    156600.19   156626.51   156605.06   156637.24   156611.04   156625.38   156635.17   156644.67   156634.81   156635.81   156690.93   156666.67   156700.49   156702.44   156705.8    156723.19   156746.24   156784.77   156783.37   156816.63   156767.56   156820.49   156805.69   156799.75   156813.77   156847.63   156837.49   156841.06   156833.21   156873.62   156877.83   156877.8    156836.4    156876.97   156889.26
16  98894.09    98661.73    98517.97    98463.94    98381.17    98335.16    98248.41    98271.91    98279.43    98235.13    98240.75    98182.86    98200.03    98201.4 98172.12    98146.85    98131.82    98103.18    98111.26    98070.4 98089.39    98103.34    98063.18    98087.61    98055.12    98101.77    98064.3 98073.7 98044.23    98032.22    98024.03    98035.75    98047.34    98065.01    98070.35    98056.62    98025.54    98091.08    98101.41    98052.04    98079.79    98094.76    98012.52    98088.28    98083.11    98091.65    98097.78    98111.77    98133.52    98135.26    98181.02    98130.98    98142.39    98103.2 98151.5 98163.1 98181.58    98161.11    98181.91    98207.14    98176.71    98194   98203.63    98178.89    98213.34    98179.43    98188.91    98209   98224.92    98202.98    98199.12    98239.48    98228.15    98251.45    98263.44    98253.43    98253.53    98293.22    98310.56    98299.46    98324.44    98304.57    98320.92    98331.45    98315.52    98316.35    98350.96    98356.69    98336.74    98322.17    98356.82    98367.58    98355.18    98342.84    98346.7 98374.95
17                                                                                                                                                                                                                                                                                                                                                                                              
12  3129.52 3147.16 3160.49 3171.33 3214.77 3236.69 3275    3280.86 3287.46 3302.41 3331.16 3375.36 3371.95 3378.69 3377.02 3373.65 3397.39 3388.79 3416.8  3457.74 3447.79 3456.51 3455.32 3495.66 3492.27 3505.96 3510.87 3533.18 3522.81 3524.65 3572.99 3575.17 3581.11 3579.16 3584.39 3601.6  3591.13 3619.1  3581.45 3597.28 3610.98 3627.1  3641.58 3639.8  3628.65 3655.72 3649.4  3648.33 3676.89 3661.96 3697.21 3689.05 3693.71 3710.29 3734.39 3732.68 3732.57 3760.45 3753.94 3778.77 3792.45 3764.17 3804.36 3804.46 3807.49 3817.46 3854.72 3820.18 3844.23 3844.19 3856.38 3856.53 3856.95 3905.39 3868.66 3898.41 3908.94 3905.1  3942.19 3941.65 3957.9  3936.05 3953.8  3952.5  3986.09 3972.33 3974.86 3962.69 4006.6  4007.01 4013.11 4036.94 3981.75 3982.69 3982.16 4017.66
13  2946817.59  2944662.04  2941123.24  2940256.46  2935558.38  2931746.01  2928978.21  2928741.48  2926931.72  2926556.07  2924090.79  2923586 2923616.24  2921712.71  2921325.33  2921606.4   2921049.04  2920501.43  2920219.82  2919483.39  2919055.94  2918261.89  2917710.17  2918281.08  2917460.36  2918447.78  2917467.81  2917025.06  2914725.09  2917582.26  2916970.03  2917224.28  2917123.34  2916758.73  2916377.49  2916374.34  2915134.44  2916170.14  2916194.3   2916438.93  2916841.46  2916923.27  2916298.61  2916843.73  2917128.84  2916505.26  2917823.72  2917249.33  2918275.17  2918657.92  2918593.87  2918092.17  2916450.26  2917383.96  2917260.84  2918251.2   2916669.62  2917421.6   2916740.6   2917259.97  2916818.62  2917508.43  2918006.44  2917757.21  2918060.88  2915855.65  2918151.83  2917179.65  2918271.42  2917682.08  2916528.9   2916751.82  2916524.81  2916802.18  2915576.36  2916073.58  2916285.37  2915885.03  2916843.4   2916897 2916226.64  2916329.2   2915110.13  2914458.88  2916433.5   2915075.24  2915229.63  2913743.19  2914563.32  2913637.85  2914569.65  2914736.45  2913404.01  2913008.04  2913627.33  2914075.7
14  230608.01   230312.12   229832.18   229627.99   229210.49   228632.32   228363.82   228331.31   228218.46   228144.81   227964.12   227953.21   228026.95   227840.86   227741.13   227753.98   227740.91   227540.28   227557.01   227318.88   227512.95   227296.66   227285.16   227307.5    227240.68   227426.04   227221.35   227178.03   226972.82   227183.08   227053.52   227103.26   227226.78   227095.99   227295.09   227357.08   226998.07   227155.45   227099.14   227256.82   227092.64   227274.71   227046.17   227211.35   227271.17   227113.9    227296.57   227410.79   227169.76   227314.2    227496.21   227252.71   227267.55   227308.58   227361.06   227333.64   227178.12   227358.18   227154.37   227278.39   227226.46   227220.65   227276.21   227336.72   227239.63   227201.77   227298.1    227286.38   227398.71   227336.91   227365.81   227255.2    227241.09   227129.99   227074.28   227152.61   227331.13   227349.15   227404.26   227317.63   227228.69   227163.85   226953.01   226922.52   227207.38   227141.31   227117.31   227162.19   227210.19   227078.05   227066.16   227226.41   226951.75   226963.37   226956.1    227106.71
15  25607.66    25705.26    25483.12    25478.82    25410.26    25384.14    25296.8 25297.31    25185.21    25310.15    25275.49    25246.13    25249.32    25322.94    25258.49    25231.09    25294.81    25282.72    25211.84    25373.49    25201.84    25277.95    25356.21    25331.92    25191.69    25268.33    25359.69    25177.89    25275.49    25293.96    25212.5 25256.51    25209.77    25207.68    25245.21    25130.97    25246.49    25073.61    25141.74    25191.55    25275.35    25218.04    25234.66    25144.64    25294.39    25197.99    25252.66    25029.12    25239.01    25241.82    25299.99    25249.92    25112.44    25140.67    25247.73    25235.76    25316.81    25188.8 25122.93    25186.04    25149.75    25127.59    25175.98    25093.1 25224.16    25166.84    25170.27    25203.5 25258.65    25192.54    25154.19    25180.35    25197.78    25340.6 25224.43    25173.91    25205.17    25253.24    25325.32    25459.4 25186.6 25238.33    25237.85    25130.99    25303.22    25188.74    25226.41    25190.27    25082.58    25059.87    25295.12    25197.11    25222.02    25208.2 25173.89    25197.88

sorry (copied from excel), the labels and data are not in line.

Basically, I want to seperate this dataframe into 8 units... so for the first unit, Unit 1, I want 12 columns.... runs 1 to 12. and so on. Unit 8 will be run 85 - 96.

I just renamed 8 dataframes and specified the columns I wanted to take from the main dataframe (df)

Here is my code for this:

df1 = df.ix[:,0:7]
df2 = df.ix[:,12:24]
df3 = df.ix[:,24:36]
df4 = df.ix[:,36:48]
df5 = df.ix[:,48:60]
df6 = df.ix[:,60:72]
df7 = df.ix[:,72:84]
df8 = df.ix[:,84:96]

pieces = (df1,df2,df3,df4,df5,df6,df7,df8)

Finally, I used concat to concatenate the 8 dataframes together (Stacked on top of each other). However, the output it somewhat distorted and it is not in order.

df_final = pd.concat(pieces, ignore_index = True)

print df_final

out:

    Run 1   Run 13  Run 14  Run 15  Run 16  Run 17  Run 18  Run 19  Run 2   Run 20  Run 21  Run 22  Run 23  Run 24  Run 25  Run 26  Run 27  Run 28  Run 29  Run 3   Run 30  Run 31  Run 32  Run 33  Run 34  Run 35  Run 36  Run 37  Run 38  Run 39  Run 4   Run 40  Run 41  Run 42  Run 43  Run 44  Run 45  Run 46  Run 47  Run 48  Run 49  Run 5   Run 50  Run 51  Run 52  Run 53  Run 54  Run 55  Run 56  Run 57  Run 58  Run 59  Run 6   Run 60  Run 61  Run 62  Run 63  Run 64  Run 65  Run 66  Run 67  Run 68  Run 69  Run 7   Run 70  Run 71  Run 72  Run 73  Run 74  Run 75  Run 76  Run 77  Run 78  Run 79  Run 80  Run 81  Run 82  Run 83  Run 84  Run 85  Run 86  Run 87  Run 88  Run 89  Run 90  Run 91  Run 92  Run 93  Run 94  Run 95  Run 96
0   5194322.07                              5195697.94                                          5195730.25                                          5196009.11                                          5195054.02                                          5193386.44                                          5192664.99                                                                                                          
1   1453304.43                              1454792.33                                          1454807.96                                          1454768.09                                          1455077.49                                          1454644.59                                          1454545.94                                                                                                          
2   193379.75                               193027.34                                           192806.25                                           192501.2                                            192602.7                                            192477.86                                           192402.72                                                                                                           
3   157553.12                               157252.95                                           157080.77                                           156941.24                                           156887.86                                           156776.95                                           156669.69                                                                                                           
4   98894.09                                98661.73                                            98517.97                                            98463.94                                            98381.17                                            98335.16                                            98248.41                                                                                                            
5                                                                                                                                                                                                                                                                                                                                                                           
6   3129.52                             3147.16                                         3160.49                                         3171.33                                         3214.77                                         3236.69                                         3275                                                                                                            
7   2946817.59                              2944662.04                                          2941123.24                                          2940256.46                                          2935558.38                                          2931746.01                                          2928978.21                                                                                                          
8   230608.01                               230312.12                                           229832.18                                           229627.99                                           229210.49                                           228632.32                                           228363.82                                                                                                           
9   25607.66                                25705.26                                            25483.12                                            25478.82                                            25410.26                                            25384.14                                            25296.8                                                                                                         
10      5190739.45  5190416.85  5189592.97  5188898.89  5188461.01  5188735.44  5189156.83      5188870.35  5188306.12  5187746.51  5188023.45  5187536.91                                                                                                                                                                                                                                                                                                                  
11      1455358.05  1455533.45  1455208.56  1454913.89  1455124.45  1454644.83  1455071.25      1454812.46  1454838.9   1454842.33  1454895.52  1454838.55                                                                                                                                                                                                                                                                                                                  
12      192331.79   192357.29   192277.84   192270.06   192232.67   192170.09   192216.06       192182.3    192163.13   192145.32   192164.63   192157.59                                                                                                                                                                                                                                                                                                                   
13      156586.19   156620.33   156558.26   156539  156501.76   156445.28   156465.98       156435.93   156436.62   156444.92   156422.48   156446.25                                                                                                                                                                                                                                                                                                                   
14      98200.03    98201.4 98172.12    98146.85    98131.82    98103.18    98111.26        98070.4 98089.39    98103.34    98063.18    98087.61                                                                                                                                                                                                                                                                                                                    
15                                                                                                                                                                                                                                                                                                                                                                          
16      3371.95 3378.69 3377.02 3373.65 3397.39 3388.79 3416.8      3457.74 3447.79 3456.51 3455.32 3495.66                                                                                                                                                                                                                                                                                                                 
17      2923616.24  2921712.71  2921325.33  2921606.4   2921049.04  2920501.43  2920219.82      2919483.39  2919055.94  2918261.89  2917710.17  2918281.08                                                                                                                                                                                                                                                                                                                  
18      228026.95   227840.86   227741.13   227753.98   227740.91   227540.28   227557.01       227318.88   227512.95   227296.66   227285.16   227307.5                                                                                                                                                                                                                                                                                                                    
19      25249.32    25322.94    25258.49    25231.09    25294.81    25282.72    25211.84        25373.49    25201.84    25277.95    25356.21    25331.92                                                                                                                                                                                                                                                                                                                    
20                                                          5188085.85  5188634.95  5187861.42  5188124.97  5187076.75      5189218.62  5189052.51  5188571.63  5188486.76  5188502.68  5188318.63  5188512.5                                                                                                                                                                                                                                                               
21                                                          1454888.25  1455024.08  1454624.57  1455159.29  1454889.65      1454906.92  1454789.36  1454579.06  1455060.12  1455108.26  1455289.8   1455269.54                                                                                                                                                                                                                                                              
22                                                          192134.08   192172.82   192098.36   192146.81   192106.65       192082.12   192057.73   192065.45   192080.46   192128.27   192096.82   192120.97                                                                                                                                                                                                                                                               
23                                                          156426.4    156447.14   156397.15   156421.85   156391.94       156370.94   156337.46   156364.73   156380.6    156399.43   156389.75   156386.21                                                                                                                                                                                                                                                               
24                                                          98055.12    98101.77    98064.3 98073.7 98044.23        98032.22    98024.03    98035.75    98047.34    98065.01    98070.35    98056.62                                                                                                                                                                                                                                                                
25                                                                                                                                                                                                                                                                                                                                                                          
26                                                          3492.27 3505.96 3510.87 3533.18 3522.81     3524.65 3572.99 3575.17 3581.11 3579.16 3584.39 3601.6                                                                                                                                                                                                                                                              
27                                                          2917460.36  2918447.78  2917467.81  2917025.06  2914725.09      2917582.26  2916970.03  2917224.28  2917123.34  2916758.73  2916377.49  2916374.34                                                                                                                                                                                                                                                              
28                                                          227240.68   227426.04   227221.35   227178.03   226972.82       227183.08   227053.52   227103.26   227226.78   227095.99   227295.09   227357.08                                                                                                                                                                                                                                                               
29                                                          25191.69    25268.33    25359.69    25177.89    25275.49        25293.96    25212.5 25256.51    25209.77    25207.68    25245.21    25130.97                                                                                                                                                                                                                                                                
30                                                                                                              5188409.83  5188250.86  5188885.18      5188999.83  5189365.09  5190159.72  5189771.1   5190136.3   5191179.72  5191256.35  5191147.97  5191712.32                                                                                                                                                                                                          
31                                                                                                              1455227.93  1455734.55  1455846     1455774.16  1456130.24  1456289.94  1455711.1   1456447.68  1456588.78  1456796.61  1456867.04  1457081.75                                                                                                                                                                                                          
32                                                                                                              192081.97   192166.45   192157.38       192121.78   192203.97   192215.73   192098.89   192181.45   192211.12   192234.93   192245.5    192282.35                                                                                                                                                                                                           
33                                                                                                              156346.02   156453.62   156442.94       156392.71   156436.89   156449.56   156363.24   156443.5    156448.72   156429.21   156479.57   156498                                                                                                                                                                                                          
34                                                                                                              98025.54    98091.08    98101.41        98052.04    98079.79    98094.76    98012.52    98088.28    98083.11    98091.65    98097.78    98111.77                                                                                                                                                                                                            
35                                                                                                                                                                                                                                                                                                                                                                          
36                                                                                                              3591.13 3619.1  3581.45     3597.28 3610.98 3627.1  3641.58 3639.8  3628.65 3655.72 3649.4  3648.33                                                                                                                                                                                                         
37                                                                                                              2915134.44  2916170.14  2916194.3       2916438.93  2916841.46  2916923.27  2916298.61  2916843.73  2917128.84  2916505.26  2917823.72  2917249.33                                                                                                                                                                                                          
38                                                                                                              226998.07   227155.45   227099.14       227256.82   227092.64   227274.71   227046.17   227211.35   227271.17   227113.9    227296.57   227410.79                                                                                                                                                                                                           
39                                                                                                              25246.49    25073.61    25141.74        25191.55    25275.35    25218.04    25234.66    25144.64    25294.39    25197.99    25252.66    25029.12                                                                                                                                                                                                            
40                                                                                                                                                                  5192430.88      5193407.95  5192603.89  5192248.7   5192197.65  5193096.79  5193005.25  5193985.8   5193451.22  5193489.16  5193562.72      5194621.43                                                                                                                                                  
41                                                                                                                                                                  1457274.68      1457155.16  1457782.73  1457065.11  1457459.15  1457347.08  1457837.54  1457999.87  1458171.82  1458241.76  1458320.08      1458622.22                                                                                                                                                  
42                                                                                                                                                                  192290.05       192278.03   192370.19   192250.39   192308.68   192264.65   192339.55   192365.62   192394.12   192385.72   192403.42       192431.52                                                                                                                                                   
43                                                                                                                                                                  156535.45       156528.7    156603.81   156486.44   156524.97   156482.7    156558.25   156574.41   156585.45   156572.05   156610.77       156638.11                                                                                                                                                   
44                                                                                                                                                                  98133.52        98135.26    98181.02    98130.98    98142.39    98103.2 98151.5 98163.1 98181.58    98161.11    98181.91        98207.14                                                                                                                                                    
45                                                                                                                                                                                                                                                                                                                                                                          
46                                                                                                                                                                  3676.89     3661.96 3697.21 3689.05 3693.71 3710.29 3734.39 3732.68 3732.57 3760.45 3753.94     3778.77                                                                                                                                                 
47                                                                                                                                                                  2918275.17      2918657.92  2918593.87  2918092.17  2916450.26  2917383.96  2917260.84  2918251.2   2916669.62  2917421.6   2916740.6       2917259.97                                                                                                                                                  
48                                                                                                                                                                  227169.76       227314.2    227496.21   227252.71   227267.55   227308.58   227361.06   227333.64   227178.12   227358.18   227154.37       227278.39                                                                                                                                                   
49                                                                                                                                                                  25239.01        25241.82    25299.99    25249.92    25112.44    25140.67    25247.73    25235.76    25316.81    25188.8 25122.93        25186.04                                                                                                                                                    
50                                                                                                                                                                                                                          5194170.84  5194198.19  5194866.16  5194030.81  5194421.67  5193745.31  5195458.37  5196342.62  5194881.29      5195036.46  5193627.87  5194470.9                                                                                               
51                                                                                                                                                                                                                          1458574.79  1458586.67  1458701.91  1458749.17  1458869.01  1458755.66  1458885 1459167.12  1458881.12      1459110.4   1458918 1459297.49                                                                                              
52                                                                                                                                                                                                                          192408.2    192414.77   192419.74   192424.98   192432.85   192422.79   192444.94   192454.58   192456.89       192449  192451.98   192507.83                                                                                               
53                                                                                                                                                                                                                          156607.3    156600.19   156626.51   156605.06   156637.24   156611.04   156625.38   156635.17   156644.67       156634.81   156635.81   156690.93                                                                                               
54                                                                                                                                                                                                                          98176.71    98194   98203.63    98178.89    98213.34    98179.43    98188.91    98209   98224.92        98202.98    98199.12    98239.48                                                                                                
55                                                                                                                                                                                                                                                                                                                                                                          
56                                                                                                                                                                                                                          3792.45 3764.17 3804.36 3804.46 3807.49 3817.46 3854.72 3820.18 3844.23     3844.19 3856.38 3856.53                                                                                             
57                                                                                                                                                                                                                          2916818.62  2917508.43  2918006.44  2917757.21  2918060.88  2915855.65  2918151.83  2917179.65  2918271.42      2917682.08  2916528.9   2916751.82                                                                                              
58                                                                                                                                                                                                                          227226.46   227220.65   227276.21   227336.72   227239.63   227201.77   227298.1    227286.38   227398.71       227336.91   227365.81   227255.2                                                                                                
59                                                                                                                                                                                                                          25149.75    25127.59    25175.98    25093.1 25224.16    25166.84    25170.27    25203.5 25258.65        25192.54    25154.19    25180.35                                                                                                
60                                                                                                                                                                                                                                                                              5195017.44  5194402.87  5194659.24  5194751.51  5195016.87  5194802.11  5195467.68  5194654.04  5195622.23  5194709.45  5195050.77  5195097.58                                              
61                                                                                                                                                                                                                                                                              1459375.28  1459338.09  1459413.22  1459726.96  1459926.75  1459943.81  1460193.37  1460242.02  1460274.7   1460319.25  1460494.5   1460347.8                                               
62                                                                                                                                                                                                                                                                              192490.77   192504.55   192520.85   192539.33   192549.03   192578.96   192618.21   192638.83   192629.15   192617.57   192651.62   192626.81                                               
63                                                                                                                                                                                                                                                                              156666.67   156700.49   156702.44   156705.8    156723.19   156746.24   156784.77   156783.37   156816.63   156767.56   156820.49   156805.69                                               
64                                                                                                                                                                                                                                                                              98228.15    98251.45    98263.44    98253.43    98253.53    98293.22    98310.56    98299.46    98324.44    98304.57    98320.92    98331.45                                                
65                                                                                                                                                                                                                                                                                                                                                                          
66                                                                                                                                                                                                                                                                              3856.95 3905.39 3868.66 3898.41 3908.94 3905.1  3942.19 3941.65 3957.9  3936.05 3953.8  3952.5                                              
67                                                                                                                                                                                                                                                                              2916524.81  2916802.18  2915576.36  2916073.58  2916285.37  2915885.03  2916843.4   2916897 2916226.64  2916329.2   2915110.13  2914458.88                                              
68                                                                                                                                                                                                                                                                              227241.09   227129.99   227074.28   227152.61   227331.13   227349.15   227404.26   227317.63   227228.69   227163.85   226953.01   226922.52                                               
69                                                                                                                                                                                                                                                                              25197.78    25340.6 25224.43    25173.91    25205.17    25253.24    25325.32    25459.4 25186.6 25238.33    25237.85    25130.99                                                
70                                                                                                                                                                                                                                                                                                                              5195987.22  5195831.3   5194776.48  5193605.12  5194317.87  5194089.21  5194563.64  5193895.14  5194140.95  5193791.85  5193915.21  5194343.34
71                                                                                                                                                                                                                                                                                                                              1460589.02  1460436.82  1460754.06  1460643.79  1460803.29  1460817.97  1460948.1   1460903.97  1460944.45  1460874.04  1460929.33  1461072.84
72                                                                                                                                                                                                                                                                                                                              192649.6    192636.68   192703.22   192661.42   192687.33   192704.48   192729.77   192731.6    192742.22   192701.82   192729.55   192743.99
73                                                                                                                                                                                                                                                                                                                              156799.75   156813.77   156847.63   156837.49   156841.06   156833.21   156873.62   156877.83   156877.8    156836.4    156876.97   156889.26
74                                                                                                                                                                                                                                                                                                                              98315.52    98316.35    98350.96    98356.69    98336.74    98322.17    98356.82    98367.58    98355.18    98342.84    98346.7 98374.95
75                                                                                                                                                                                                                                                                                                                                                                          
76                                                                                                                                                                                                                                                                                                                              3986.09 3972.33 3974.86 3962.69 4006.6  4007.01 4013.11 4036.94 3981.75 3982.69 3982.16 4017.66
77                                                                                                                                                                                                                                                                                                                              2916433.5   2915075.24  2915229.63  2913743.19  2914563.32  2913637.85  2914569.65  2914736.45  2913404.01  2913008.04  2913627.33  2914075.7
78                                                                                                                                                                                                                                                                                                                              227207.38   227141.31   227117.31   227162.19   227210.19   227078.05   227066.16   227226.41   226951.75   226963.37   226956.1    227106.71
79                                                                                                                                                                                                                                                                                                                              25303.22    25188.74    25226.41    25190.27    25082.58    25059.87    25295.12    25197.11    25222.02    25208.2 25173.89    25197.88

This is my problem. I want the dataframes in line and stacked on top of each other.

like image 279
Joey Avatar asked Jun 15 '15 08:06

Joey


People also ask

How do I stack two pandas DataFrames?

When we concatenate DataFrames, we need to specify the axis. axis=0 tells pandas to stack the second DataFrame UNDER the first one. It will automatically detect whether the column names are the same and will stack accordingly. axis=1 will stack the columns in the second DataFrame to the RIGHT of the first DataFrame.

How do you stack two DataFrames on top of each other in R?

Using rbind() to merge two R data frames This function stacks the two data frames on top of each other, appending the second data frame to the first. For this function to operate, both data frames need to have the same number of columns and the same column names.

How do I merge 3 data frames in pandas?

Pandas merge() function is used to merge multiple Dataframes. We can use either pandas. merge() or DataFrame. merge() to merge multiple Dataframes.

How do you combine data frames?

Another way to combine DataFrames is to use columns in each dataset that contain common values (a common unique id). Combining DataFrames using a common field is called “joining”. The columns containing the common values are called “join key(s)”.


1 Answers

You're very nearly there.

The problem is that the column names are all different within each sub dataframe. Thus, when pandas does the concat, it doesn't just append the dataframes to the bottom, it expands the dataframe to have new colums with the right names and then appends the rows.

You can solve this by renaming the columns in the sub dataframes e.g.

for sub_df in pieces:
    sub_df.columns=range(12)

N.B. df2 to df8 contain what you want, I think. For some reason, you've made df1 contain only the first 7 columns, rather than 12. I'll assume that's a typo for now.

Resulting in full working code (I copied your input data into a file named 'data1.csv')

import pandas as pd
import numpy as np    

df = pd.read_csv('data1.csv')

df1 = df.ix[:,0:12]
df2 = df.ix[:,12:24]
df3 = df.ix[:,24:36]
df4 = df.ix[:,36:48]
df5 = df.ix[:,48:60]
df6 = df.ix[:,60:72]
df7 = df.ix[:,72:84]
df8 = df.ix[:,84:96]

pieces = (df1,df2,df3,df4,df5,df6,df7,df8)

# Give the columns the same labels in each sub dataframe
# I've used numbers for convenience - you can give more descriptive names if you want
for sub_df in pieces:
    sub_df.columns=range(12)

df_final = pd.concat(pieces, ignore_index = True)

print df_final

Final note on ordering

You note the unexpected ordering of your columns in your example. This won't affect my solution, but I will explain it for completeness.

The columns in your output are in what is called 'Lexicographic ordering'. This is a common problem when sorting strings containing numbers in Python (and other languages). They are sorted in an order that looks almost right, but somehow runs 1, 10, 11 ... 19, 2, 20 and so on. That is because the ordering sorts letter by letter like a dictionary, but with 0 to 9 coming before a

like image 73
J Richard Snape Avatar answered Nov 14 '22 23:11

J Richard Snape