python - polars unpivot columns then rows - Stack Overflow

During solving Excel BI challengeI used Table.UnpivotOtherColumns(Source, {}, "Attribute", &q

During solving Excel BI challenge

I used Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value"). And this gave me an output:

Next I was trying to solve the same challenge in polars and I used unpivot expression:

import polars as pl,datetime
dc={'Hall': ['Hall1', 'Hall1', 'Hall2', 'Hall2'],
 'Date': [datetime.date(2023, 2, 1),
  datetime.date(2023, 2, 2),
  datetime.date(2023, 2, 1),
  datetime.date(2023, 2, 4)],
 'Guest1': ['A', 'X', 'R', 'S'],
 'Guest2': ['B', 'Y', None, 'P'],
 'Guest3': [None, 'Z', None, None],
 'Guest4': [None, 'Q', None, None]}
df=pl.from_dict(dc).unpivot()
df

and this gave me output:

Is it possible to unpivot table with polars the same way as in power query ? I was thinking about iterating through each row and unpivoting each row separately and combining rows w hatack. But will it be efficient? I also checked pandas behavior and it works the same. Thanks in advance for any explanation. Artur

During solving Excel BI challenge

I used Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value"). And this gave me an output:

Next I was trying to solve the same challenge in polars and I used unpivot expression:

import polars as pl,datetime
dc={'Hall': ['Hall1', 'Hall1', 'Hall2', 'Hall2'],
 'Date': [datetime.date(2023, 2, 1),
  datetime.date(2023, 2, 2),
  datetime.date(2023, 2, 1),
  datetime.date(2023, 2, 4)],
 'Guest1': ['A', 'X', 'R', 'S'],
 'Guest2': ['B', 'Y', None, 'P'],
 'Guest3': [None, 'Z', None, None],
 'Guest4': [None, 'Q', None, None]}
df=pl.from_dict(dc).unpivot()
df

and this gave me output:

Is it possible to unpivot table with polars the same way as in power query ? I was thinking about iterating through each row and unpivoting each row separately and combining rows w hatack. But will it be efficient? I also checked pandas behavior and it works the same. Thanks in advance for any explanation. Artur

Share Improve this question edited Dec 10, 2024 at 11:40 jqurious 21.7k5 gold badges20 silver badges39 bronze badges asked Nov 20, 2024 at 19:22 ArtupArtup 798 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

If I understand your problem correctly your issue is only with the final ordering of the rows in the final dataframe, you can add an index to sort with by adding a row index column and unpivoting on this column, then sorting by the index and removing the column afterward so it matches the desired dataframe. Just changing your last line of code:

import polars as pl,datetime
dc={'Hall': ['Hall1', 'Hall1', 'Hall2', 'Hall2'],
 'Date': [datetime.date(2023, 2, 1),
  datetime.date(2023, 2, 2),
  datetime.date(2023, 2, 1),
  datetime.date(2023, 2, 4)],
 'Guest1': ['A', 'X', 'R', 'S'],
 'Guest2': ['B', 'Y', None, 'P'],
 'Guest3': [None, 'Z', None, None],
 'Guest4': [None, 'Q', None, None]}
df=pl.from_dict(dc).with_row_index().unpivot(index = ["index"], variable_name = "Attribute").drop_nulls().sort("index").drop("index")
df

produces the following dataframe

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742335119a4424495.html

相关推荐

  • python - polars unpivot columns then rows - Stack Overflow

    During solving Excel BI challengeI used Table.UnpivotOtherColumns(Source, {}, "Attribute", &q

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信