我正在使用以下数据帧This is only a portion of the df
我想创建一个多索引数据帧,其中顶级索引是title_no,子索引是共享相同title_no的所有release_no值。
我尝试过使用groupby.apply方法,但这会将具有相同title_no的release_no's分组到列表中,并删除其余列:
df = pd.DataFrame(df.groupby('title_no')['release_no'].unique()).reset_index()理想情况下,我希望我的数据帧看起来像这样:
title_no release_no name country_id
199034
732644 Jurassic Park III ES
891376 Jurassic Park III CA
732658 Jurassic Park III TH
199052
1119213 Myth of Fingerprints IT
925041 Myth of Fingerprints ES
448432 Myth of Fingerprints US
564033 Myth of Fingerprints FR
...有没有办法在pandas中做到这一点,我可以列出同一title_no下的行,并能够使用title_no在一级对这些行进行索引,然后使用release_no在较低级别对这些行进行索引?
发布于 2019-03-22 10:41:55
为此您不需要groupby;排序就足够了:
df.sort_values(['title_no', 'release_no']).set_index(['title_no', 'release_no'])https://stackoverflow.com/questions/55291998
复制相似问题