Pandas: Creating DataFrame from Series

前端 未结 3 1136
清酒与你
清酒与你 2020-12-29 01:36

My current code is shown below - I\'m importing a MAT file and trying to create a DataFrame from variables within it:

mat = loadmat(file_path)  # load mat-fi         


        
3条回答
  •  情歌与酒
    2020-12-29 02:11

    I guess anther way, possibly faster, to achieve this is 1) Use dict comprehension to get desired dict (i.e., taking 2nd col of each array) 2) Then use pd.DataFrame to create an instance directly from the dict without loop over each col and concat.

    Assuming your mat looks like this (you can ignore this since your mat is loaded from file):

    In [135]: mat = {'a': np.random.randint(5, size=(4,2)),
       .....: 'b': np.random.randint(5, size=(4,2))}
    
    In [136]: mat
    Out[136]: 
    {'a': array([[2, 0],
            [3, 4],
            [0, 1],
            [4, 2]]), 'b': array([[1, 0],
            [1, 1],
            [1, 0],
            [2, 1]])}
    

    Then you can do:

    In [137]: df = pd.DataFrame ({name:mat[name][:,1] for name in mat})
    
    In [138]: df
    Out[138]: 
       a  b
    0  0  0
    1  4  1
    2  1  0
    3  2  1
    
    [4 rows x 2 columns]
    

提交回复
热议问题