1

I have an issue importing data from an excel file with nested column titles. Several column names are integers and I would like to have them as strings.

Let's say I have this table in excel:

|     | 1                 | string_name       |                   |
|-----|-------------------|-------------------|-------------------|
| cat | value1            | value2            | value3            |
| A   | 0,972040109825603 | 0,056557228055112 | 0,976955685101913 |
| B   | 0,320747613034341 | 0,149341390123682 | 0,638191659714267 |
| C   | 0,790582690075218 | 0,72042597879107  | 0,001334403836215 |
| D   | 0,536830294783296 | 0,374625041462985 | 0,400407699629966 |
| E   | 0,407865892894399 | 0,622162974355068 | 0,374418521692358 |

I import it as a dataframe

df = pd.read_excel('expl.xlsm', header=[0, 1])

print(df)

which gives

            1           string_name
cat    value1    value2      value3
A    0.972040  0.056557    0.976956
B    0.320748  0.149341    0.638192
C    0.790583  0.720426    0.001334
D    0.536830  0.374625    0.400408
E    0.407866  0.622163    0.374419

IN:

df.columns

OUT:

MultiIndex(levels=[[1, 'string_name'], ['value1', 'value2', 'value3']],
       labels=[[0, 0, 1], [0, 1, 2]],
       names=[None, 'cat'])

So I want to convert 1 to a '1'. Or ideally import the dataframe with only string-type column names in the first place.

I get the column values of the first level by

df.columns.get_level_values(0)

OUT:

Index([1, 1, 'string_name'], dtype='object')

But

df.columns.get_level_values(0) = df.columns.get_level_values(0).astype(str)

returns an error:

df.columns.get_level_values(0) = df.columns.get_level_values(0).astype(str)                                                                        

SyntaxError: can't assign to function call

How can I change the datatype of the column names or import the data with only string column titles at the first place?

hendriksc1
  • 59
  • 5

1 Answers1

1

You need create new MultiIndex and assign back:

a = df.columns.get_level_values(0).astype(str)
b = df.columns.get_level_values(1)

df.columns = pd.MultiIndex.from_arrays([a,b], names=df.columns.names)

print (df.columns)
MultiIndex(levels=[['1', 'string_name'], ['cat', 'value1', 'value2']],
           labels=[[0, 0, 1], [0, 1, 2]],
           names=[None, 'cat'])

If want filter only strings values in first level of MultiIndex is possible create boolean mask and filter by boolean indexing with loc:

mask = df.columns.get_level_values(0).map(lambda x: isinstance(x, str))

df1 = df.loc[:, mask]
print (df1)
  string_name
       value2
A    0.976956
B    0.638192
C    0.001334
D    0.400408
E    0.374419
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252