0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

`pandas.DataFrame`の`columns`引数にtupleのlistを渡しても、MultiIndexな列にはらない

Posted at

環境

  • Python 3.11.2
  • pandas 2.0.2

起きたこと

pandas.DataFrameにkeyがtupleであるdictを渡すと、MultiIndexな列を持つDataFrameを生成できます。

In [94]: data = {("japan","male"):[1,2,3], ("japan","female"):[11,12,13], ("u.s.","male"):[2,3,4]}

In [94]: data
Out[95]: 
{('japan', 'male'): [1, 2, 3],
 ('japan', 'female'): [11, 12, 13],
 ('u.s.', 'male'): [2, 3, 4]}

In [96]: df1 = pandas.DataFrame(data)

In [97]: df1
Out[97]: 
  japan        u.s.
   male female male
0     1     11    2
1     2     12    3
2     3     13    4

In [98]: df1.columns
Out[98]: 
MultiIndex([('japan',   'male'),
            ('japan', 'female'),
            ( 'u.s.',   'male')],
           )

生成されたDataFrameの列を絞り込むため、columns引数に列名を指定します。
以下はcolumns引数の説明です。

Column labels to use for resulting frame when data does not have them, defaulting to RangeIndex(0, 1, 2, …, n). If data contains column labels, will perform column selection instead. 1

columns引数にtupleのlistを渡したら、生成されたDataFrameの列はMultiIndexではなく通常のIndexになってしまいました。

In [102]: df2 = pandas.DataFrame(data, columns=[("japan","female"),  ("u.s.","male")])

In [103]: df2
Out[103]: 
   (japan, female)  (u.s., male)
0               11             2
1               12             3
2               13             4

In [104]: df2.columns
Out[104]: Index([('japan', 'female'), ('u.s.', 'male')], dtype='object')

解決

MultiIndexの列を生成するには、columns引数にMultiIndexを渡す必要がありました。

In [107]: target_columns = pandas.MultiIndex.from_tuples([("japan","female"),  ("u.s.","male")])

In [108]: target_columns
Out[108]: 
MultiIndex([('japan', 'female'),
            ( 'u.s.',   'male')],
           )

In [109]: df3 = pandas.DataFrame(data, columns=target_columns)

In [110]: df3
Out[110]: 
   japan u.s.
  female male
0     11    2
1     12    3
2     13    4

In [111]: df3.columns
Out[111]: 
MultiIndex([('japan', 'female'),
            ( 'u.s.',   'male')],
           )
  1. https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?