简体   繁体   中英

pandas - load matrix style csv directly to pivottable object

I have serialized a pivotTable object using pandas .to_csv function. now i want to load it back to a pivotTable object.

The CSV looks something like this, the first column and row are id's

        49033   49967   50221   52301
41619               
41665               
46217               
46291   0.002           
47749               
49033           0.001            0.006
49967                    0.03
50221               

To my knowledge pandas doesn't have any function that can do this for me. Do I have to parse the whole table by myself ?

use pd.read_fwf() if you have fixed width format file.

Demo:

In [28]: fn = r'D:\temp\.data\1.fwf'

In [29]: pd.read_fwf(fn).fillna('')
Out[29]:
   Unnamed: 0  49033  49967 50221  52301
0       41619
1       41665
2       46217
3       46291  0.002
4       47749
5       49033         0.001        0.006
6       49967                0.03
7       50221

If you have a regular CSV file - use pd.read_csv()

Demo:

In [32]: pd.read_csv(fn, index_col=0).fillna('')
Out[32]:
            49033       49967      50221       52301       53627      53733       53779      54095       54871      55059     55921  \
41619
41665                                                                        0.00631642
46217
46291  0.00256985                                                             0.0134134
47749                                                             0.0148866
49033              0.00188816             0.00680558  0.00155183             0.00735491             0.00293469  0.0028227
49967                          0.0318261
50221
52301
52465
52621                                                                                                                      0.165127
52851
53407                                                             0.0289802
53437
53627
53733
53779                                                                                    0.0311593
53955
54065

           56007       56163       56287
41619
41665  0.0374982               0.0125017
46217
46291
47749
49033             0.00318618
49967
50221
52301
52465
52621
52851
53407
53437
53627
53733
53779                         0.00455196
53955
54065                          0.0274595

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM