2017-12-01 2 views
1

パンダのgroupbyデータフレームをCSVに出力したい。さまざまなStackOverflowソリューションを試しましたが、うまく機能しませんでした。パンダグループからto_csv

のPython 3.6.1、パンダ0.20.1

GROUPBY結果は次のようになります。

week_grouped = df.groupby('week') 
week_grouped.sum() #At this point you have the groupby result 
week_grouped.to_csv('week_grouped.csv') #Can't do this - .to_csv is not a df function. 

id month year count 
week     
0 9066 82 32142 895 
1 7679 84 30112 749 
2 8368 126 42187 872 
3 11038 102 34165 976 
4 8815 117 34122 767 
5 10979 163 50225 1252 
6 8726 142 38159 996 
7 5568 63 26143 582 

week count 
0 895 
1 749 
2 872 
3 976 
4 767 
5 1252 
6 996 
7 582 

現在のコードのように見えるCSVをしたいです

読み取りSO soluション:

output groupby to csv file pandas

week_grouped.drop_duplicates().to_csv('week_grouped.csv') 

結果:はAttributeError:呼び出し可能な属性にアクセスすることはできません 'drop_duplicates' 'DataFrameGroupBy' オブジェクトの、 '適用' 方法

Python pandas - writing groupby output to file

week_grouped.reset_index().to_csv('week_grouped.csv') 
を使用してみてください

結果: AttributeError: "DataFrameGroupByオブジェクトの呼び出し可能属性 'reset_index'にアクセスできません。 'apply'メソッドを使用してください。

答えて

2

2行目をweek_grouped = week_grouped.sum()に変更し、3行すべてを再実行してください。

あなたは、独自のJupyterノートセルにweek_grouped.sum()を実行する場合は、あなたが表示されますどのように代わりweek_groupedに戻って結果を割り当てる声明戻りセルの出力に出力し、。パンダのメソッドの中には、のようなinplace=Trueという引数がありますが、sumのものはありません。

EDIT:あなたのCSVで毎週番号が1回だけ表示されますか?

df = pd.read_csv('input.csv') 
df[['id', 'count']].to_csv('output.csv') 
+0

同じであることを意味しています。この場合、groupbyは一緒に週を集めるために使用されているので、1週間にカウントが実行できます。 – kalmdown

+1

ところで、「sum」がなぜ問題なのかを説明してくれてありがとう。 – kalmdown

0

私はGROUPBYを使用する必要はないと感じて、あなただけのあなたもしたくない列を削除することができますもしそうなら、ここgroupbyを使用しない簡単なソリューションです。

df = df.drop(['month','year'],axis==1) 
df.reset_index() 
df.to_csv('Your path') 
+0

"axis = 1"にする必要があります。ただし、行は出力されますが、週や状態ではグループ化されません。 – kalmdown

0

これをやってみてください。

week_grouped = df.groupby('week') 
week_grouped.sum().reset_index().to_csv('week_grouped.csv') 

全体をファイルにデータフレームを書くだろうと。あなただけにして、それらの2つの列をしたい場合は、ここで

week_grouped = df.groupby('week') 
week_grouped.sum().reset_index()[['week', 'count']].to_csv('week_grouped.csv') 

は、元のコードの行の説明によるラインです:

# This creates a "groupby" object (not a dataframe object) 
# and you store it in the week_grouped variable. 
week_grouped = df.groupby('week') 

# This instructs pandas to sum up all the numeric type columns in each 
# group. This returns a dataframe where each row is the sum of the 
# group's numeric columns. You're not storing this dataframe in your 
# example. 
week_grouped.sum() 

# Here you're calling the to_csv method on a groupby object... but 
# that object type doesn't have that method. Dataframes have that method. 
# So we should store the previous line's result (a dataframe) into a variable 
# and then call its to_csv method. 
week_grouped.to_csv('week_grouped.csv') 

# Like this: 
summed_weeks = week_grouped.sum() 
summed_weeks.to_csv('...') 

# Or with less typing simply 
week_grouped.sum().to_csv('...') 
+1

ありがとう! - sum()がto_csvステートメントの一部であるが、sum()がそれ自身の行で完了していないときはなぜ機能するのですか? – kalmdown

+0

回答を更新します –

0

グループキーは、グループの識別子とあるリターンキー、値のペアにより、値はグループそのもの、つまりキーと一致する元のdfのサブセットです。あなたの例では

week_grouped = df.groupby('week')は、次のように詳細に探索することができますグループ(pandas.core.groupby.DataFrameGroupByオブジェクト)の設定されている:

for k, gr in week_grouped: 
    # do your stuff instead of print 
    print(k) 
    print(type(gr)) # This will output <class 'pandas.core.frame.DataFrame'> 
    print(gr) 
    # You can save each 'gr' in a csv as follows 
    gr.to_csv('{}.csv'.format(k)) 

または代わりにあなたがグループ化されたオブジェクトの集約関数を計算することができます

この例では、デフォルトでpandasオブジェクトが不変なので、関数の結果に変数を割り当てる必要があります。

some_variable = week_grouped.sum() 
some_variable.to_csv('week_grouped.csv') # This will work 

基本的にresult.csvとweek_grouped.csvは週に複数の行に表示され、元のデータで

+0

詳細についてはありがとうございます。問題だけでなく、システムを理解するのに役立ちます。 – kalmdown

関連する問題