关于python:如何将具有NaN的合并Excel单元格读取到Pandas DataFrame中

How to read merged Excel cells with NaN into Pandas DataFrame

我想将Excel工作表读入Pandas DataFrame。但是,存在合并的Excel单元格和空行(填充了完整/部分NaN),如下所示。为了澄清,John H.已下令购买从"保镖"到"红色药丸蓝调"的所有专辑。

Excel sheet capture

当我将此Excel工作表读取到Pandas DataFrame中时,Excel数据无法正确传输。熊猫将合并的单元格视为一个单元格。 DataFrame如下所示:(注意:()中的值是我想要的值)

Dataframe capture

请注意,最后一行不包含合并的单元格;它仅带有Artist列的值。

编辑:
我确实尝试了以下操作来向前填充NaN值:(熊猫:使用合并的单元格读取Excel)

1
df.index = pd.Series(df.index).fillna(method='ffill')

但是,NaN值仍然保留。我可以使用什么策略或方法正确填充DataFrame?是否有一种Pandas方法来合并单元格并复制相应的内容?


您尝试引用的链接只需要向前填充索引列。 对于您的用例,所有数据框列都需要fillna。 因此,只需向前填充整个数据框:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
df = pd.read_excel("Input.xlsx")
print(df)

#    Order_ID Customer_name            Album_Name           Artist  Quantity
# 0       NaN           NaN            RadioShake              NaN       NaN
# 1       1.0       John H.         The Bodyguard  Whitney Houston       2.0
# 2       NaN           NaN              Lemonade          Beyonce       1.0
# 3       NaN           NaN  The Thrill Of It All        Sam Smith       2.0
# 4       NaN           NaN              Thriller  Michael Jackson      11.0
# 5       NaN           NaN                Divide       Ed Sheeran       4.0
# 6       NaN           NaN            Reputation     Taylor Swift       3.0
# 7       NaN           NaN        Red Pill Blues         Maroon 5       5.0

df = df.fillna(method='ffill')
print(df)

#    Order_ID Customer_name            Album_Name           Artist  Quantity
# 0       NaN           NaN            RadioShake              NaN       NaN
# 1       1.0       John H.         The Bodyguard  Whitney Houston       2.0
# 2       1.0       John H.              Lemonade          Beyonce       1.0
# 3       1.0       John H.  The Thrill Of It All        Sam Smith       2.0
# 4       1.0       John H.              Thriller  Michael Jackson      11.0
# 5       1.0       John H.                Divide       Ed Sheeran       4.0
# 6       1.0       John H.            Reputation     Taylor Swift       3.0
# 7       1.0       John H.        Red Pill Blues         Maroon 5       5.0

使用条件的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import pandas as pd

df_excel = pd.ExcelFile('Sales.xlsx')
df = df_excel.parse('Info')

for col in list(df):  # All columns
    pprow = 0
    prow = 1
    for row in df[1:].iterrows():  # All rows, except first
        if pd.isnull(df.loc[prow, 'Album Name']):  # If this cell is empty all in the same row too.
            continue
        elif pd.isnull(df.loc[prow, col]) and pd.isnull(df.loc[row[0], col]):  # If a cell and next one are empty, take previous valor.
            df.loc[prow, col] = df.loc[pprow, col]
        pprow = prow
        prow = row[0]

输出(我使用不同的名称):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    Order_ID Customer_name    Album Name
0        NaN           NaN         Radio
1        1.0          John            a
2        1.0          John             b
3        1.0          John             c
4        1.0          John             d
5        1.0          John             e
6        1.0          John             f
7        NaN           NaN            GE
8        2.0         Harry   We are Born
9        3.0        Lizzy        Relapse
10       4.0           Abe         Smoke
11       4.0           Abe       Tell me
12       NaN           NaN           NaN
13       NaN           NaN      Best Buy
14       5.0        Kristy      The wall
15       6.0         Sammy  Kind of blue