解析Excel文件中的数据以创建数据框

时间:2018-12-14 05:26:24

标签: python pandas parsing dataframe

我正在分析来自excel文件的数据。 我想通过使用python从excel解析数据来创建数据框架。 我的excel文件中的数据如下所示:

enter image description here

以黄色突出显示的第一行包含匹​​配项,这将是我要创建的数据框中的列之一。

实际上,第二行和第四行是我要在新数据框中创建的列的名称。 第三行和第五行是每列的值。

此处的示例仅适用于一场比赛。

我在excel文件中有多个匹配项。

我想创建一个数据框,其中包含列匹配和文件中所有蓝色的名称。 我已附加了包含多个匹配项的示例文件。 Download the file here.

我期望的数据帧是

Match                  1-0  2-0  2-1  3-0  3-1  3-2  4-0  4-1  4-2  4-3.......

MOL Vivi -vs- Chelsea  14   42   20   170  85    85  225  225 225 .....

有人可以建议我如何解析Excel数据并将其转换为数据框吗?

谢谢, 压缩

1 个答案:

答案 0 :(得分:0)

使用:

import pandas as pd
from datetime import datetime

df = pd.read_excel('test_match.xlsx')

#mask for check a-z in column HOME -vs- AWAY
m1 = df['HOME -vs- AWAY'].str.contains('[a-z]', na=False)
#create index by matches
df.index = df['HOME -vs- AWAY'].where(m1).ffill()
df.index.name = 'Match'
#remove same index and HOME -vs- AWAY column rows
df = df[df.index != df['HOME -vs- AWAY']].copy()

#test if datetime or string
m2 = df['HOME -vs- AWAY'].apply(lambda x: isinstance(x, datetime))
m3 = df['HOME -vs- AWAY'].apply(lambda x: isinstance(x, str))

#seelct next rows and set new columns names
df1 = df[m2.shift().fillna(False)]
df1.columns = df[m2].iloc[0]

#also remove only NaNs columns
df2 = df[m3.shift().fillna(False)].dropna(axis=1, how='all')
df2.columns = df[m3].iloc[0].dropna()

#join together
df = pd.concat([df1, df2], axis=1).astype(float).reset_index().rename_axis(None, axis=1)

print (df.head())
                            Match  2000-01-01 00:00:00  2000-02-01 00:00:00  \
0           MOL Vidi -vs- Chelsea                14.00                42.00   
1  Lazio -vs- Eintracht Frankfurt                 8.57                11.55   
2       Sevilla -vs- FC Krasnodar                 7.87                 6.63   
3  Villarreal -vs- Spartak Moscow                 7.43                 7.03   
4           Rennes -vs- FC Astana                 4.95                 6.38   

   2018-02-01 00:00:00  2000-03-01 00:00:00  2018-03-01 00:00:00  \
0                20.00               170.00                85.00   
1                 7.87                23.80                15.55   
2                 7.87                 8.72                 8.65   
3                 7.07                10.00                 9.43   
4                 7.33                12.00                13.20   

   2018-03-02 00:00:00  2000-04-01 00:00:00  2018-04-01 00:00:00  \
0                 85.0               225.00               225.00   
1                 21.3                64.30                42.00   
2                 25.9                14.80                14.65   
3                 23.9                19.35                17.65   
4                 38.1                31.50                34.10   

   2018-04-02 00:00:00         ...            0-1    0-2  2018-01-02 00:00:00  \
0                225.0         ...            5.6   6.80                 7.00   
1                 55.7         ...           11.0  19.05                10.45   
2                 38.1         ...           28.0  79.60                29.20   
3                 38.4         ...           20.9  58.50                22.70   
4                 81.4         ...           12.9  42.80                22.70   

     0-3  2018-01-03 00:00:00  2018-02-03 00:00:00    0-4  \
0   12.5                 12.0                 32.0   30.0   
1   48.4                 27.4                 29.8  167.3   
2  223.0                110.0                 85.4  227.5   
3  203.5                 87.6                 73.4  225.5   
4  201.7                 97.6                103.6  225.5   

   2018-01-04 00:00:00  2018-02-04 00:00:00  2018-03-04 00:00:00  
0                 29.0                 60.0                220.0  
1                 91.8                102.5                168.3  
2                227.5                227.5                227.5  
3                225.5                225.5                225.5  
4                225.5                225.5                225.5  

[5 rows x 27 columns]