質問:groupby列のパーセンテージを計算し、降順で並べ替えるにはどうすればよいですか?
必要な出力:
country count percentage
United States 2555 45%
India 923 12%
United Kingdom 397 4%
Japan 226 3%
South Korea 183 2%
私はいくつかの調査を行い、Pandasのドキュメントを調べ、Stackoverflowで運が悪かった他の質問を調べました。
私は以下を試しました:
#1試してみてください:
Df2 = df.groupby('country')['show_id'].count().nlargest()
df3 = df2.groupby(level=0).apply(lambda x: x/x.sum() * 100)
出力:
director
A. L. Vijay 100.0
A. Raajdheep 100.0
A. Salaam 100.0
A.R. Murugadoss 100.0
Aadish Keluskar 100.0
...
Çagan Irmak 100.0
Ísold Uggadóttir 100.0
Óskar Thór Axelsson 100.0
Ömer Faruk Sorak 100.0
Şenol Sönmez 100.0
Name: show_id, Length: 4049, dtype: float64
#2試してみてください:
df2 = df.groupby('country')['show_id'].count()
df2['percentage'] = df2['show_id']/6000
出力:
KeyError: 'show_id'
データセットのサンプル:
import pandas as pd
df = pd.DataFrame({
'show_id':['81145628','80117401','70234439'],
'type':['Movie','Movie','TV Show'],
'title':['Norm of the North: King Sized Adventure',
'Jandino: Whatever it Takes',
'Transformers Prime'],
'director':['Richard Finn, Tim Maltby',NaN,NaN],
'cast':['Alan Marriott, Andrew Toth, Brian Dobson',
'Jandino Asporaat','Peter Cullen, Sumalee Montano, Frank Welker'],
'country':['United States, India, South Korea, China',
'United Kingdom','United States'],
'date_added':['September 9, 2019',
'September 9, 2016',
'September 8, 2018'],
'release_year':['2019','2016','2013'],
'rating':['TV-PG','TV-MA','TV-Y7-FV'],
'duration':['90 min','94 min','1 Season'],
'listed_in':['Children & Family Movies, Comedies',
'Stand-Up Comedy','Kids TV'],
'description':['Before planning an awesome wedding for his',
'Jandino Asporaat riffs on the challenges of ra',
'With the help of three human allies, the Autob']})
これは、「country」フィールドに複数の国がある行には対応していませんが、以下の行は質問の他の部分でも機能するはずです。
初期データフレームを作成します。
df = pd.DataFrame({
'show_id':['81145628','80117401','70234439'],
'type':['Movie','Movie','TV Show'],
'title':['Norm of the North: King Sized Adventure',
'Jandino: Whatever it Takes',
'Transformers Prime'],
'director':['Richard Finn, Tim Maltby',0,0],
'cast':['Alan Marriott, Andrew Toth, Brian Dobson',
'Jandino Asporaat','Peter Cullen, Sumalee Montano, Frank Welker'],
'country':['United States, India, South Korea, China',
'United Kingdom','United States'],
'date_added':['September 9, 2019',
'September 9, 2016',
'September 8, 2018'],
'release_year':['2019','2016','2013'],
'rating':['TV-PG','TV-MA','TV-Y7-FV'],
'duration':['90 min','94 min','1 Season'],
'listed_in':['Children & Family Movies, Comedies',
'Stand-Up Comedy','Kids TV'],
'description':['Before planning an awesome wedding for his',
'Jandino Asporaat riffs on the challenges of ra',
'With the help of three human allies, the Autob']})
国別グループ:
df2 = df.groupby(by="country", as_index=False)['show_id']\
.agg('count')
agg列の名前を変更します。
df2 = df2.rename(columns={'show_id':'count'})
パーセンテージ列を作成します。
df2['percent'] = (df2['count']*100)/df2['count'].sum()
降順で並べ替え:
df2 = df2.sort_values(by='percent', ascending=False)
試行#1の問題の一部は、groupby関数に「by」パラメーターを含めなかったことである可能性があります。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加