Учитывая вашу конкретную структуру данных:
df.columns = df.iloc[0, :] # Rename the columns based on the first row of data.
df.columns.name = None # Set the columns name to None.
df = df.iloc[1:, :].reset_index(drop=True) # Drop the column names from the data in the dataframe.
>>> df.replace('NAN', np.nan).dropna(how='all', axis=1).replace(np.nan, 'Not Listed')
Name Amount Percentage
0 A 28223 8.70%
1 B Not Listed Not Listed
2 C Not Listed Not Listed
3 D 21871 6.80%
4 E Not Listed Not Listed
5 F Not Listed Not Listed
6 G 21380 6.64%
7 H Not Listed Not Listed
8 I Not Listed Not Listed
9 J 20784 6.46%
10 K Not Listed Not Listed
11 L Not Listed Not Listed
Вы можете установить индекс на имя, если хотите, изменив последнюю строку кода:
>>> >>> df.replace('NAN', np.nan).dropna(how='all', axis=1).replace(np.nan, 'Not Listed').set_index('Name')
Amount Percentage
Name
A 28223 8.70%
B Not Listed Not Listed
C Not Listed Not Listed
D 21871 6.80%
E Not Listed Not Listed
F Not Listed Not Listed
G 21380 6.64%
H Not Listed Not Listed
I Not Listed Not Listed
J 20784 6.46%
K Not Listed Not Listed
L Not Listed Not Listed