pandasの使い方がおかしいかもしれないのでそうであった場合指摘してくださるとありがたいです
python
1quantity = data.groupby(['Retailer country', 'Year', 'Product type'])['Quantity'].sum() 2revenue = data.groupby(['Retailer country', 'Year', 'Product type'])['Revenue'].sum() 3 4df = pd.DataFrame({'Quantity' : quantity, 'Revenue' : revenue}) 5df = df.loc['United States']
これあるデータからQuantity,Revenueそれぞれの合計のシリーズからDataFrameを生成したものなのですが
python
1''' 2 Quantity Revenue 3Year Product type 42012 Binoculars 45250 5244041.91 5 Climbing Accessories 275046 3843823.21 6 Cooking Gear 557640 11234915.19 7 Eyewear 958778 38649157.55 8... 92014 Binoculars 42881 5571199.91 10 Climbing Accessories 358452 4800881.44 11 Cooking Gear 455717 8984366.21 12 Eyewear 922067 43449153.73
この状態から元のデータフレームのカラムであったYearなどでフィルターする方法はありますでしょうか
例えばYearが2014の値のものだけ取り出したいのですがやり方が分からず困っています
また、別の例を挙げると
python
1quantity = data.groupby(['Retailer country', 'Product type', 'Year'])['Quantity'].sum() 2print(quantity['United States']) 3
これはgroupbyしてそれぞれのデータのQuantityの合計を出したのですが
python
1''' 2Product type Year 3Binoculars 2012 45250 4 2013 53861 5 2014 42881 6...
ここからYear が2014の値のものだけ取り出したいのですがやり方が分かりません
また、なぜ
python
1quantity['United States']
これは取り出せるのに同じやり方でさらにフィルタして取り出そうとするとErrorが起きるのか、教えていただけるとありがたいです
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/05 05:21