我有以下腳本,我想要一個簡單的GROUP BY:你如何在熊貓中只返回一個羣組?
# import the pandas module
import pandas as pd
from openpyxl import load_workbook
writer = pd.ExcelWriter(r'D:\temp\test.xlsx', engine='openpyxl')
# Create an example dataframe
raw_data = {'Date': ['2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13','2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13', '2016-05-13'],
'Portfolio': ['A', 'A', 'A', 'A', 'A', 'A', 'B', 'B','B', 'B', 'B', 'C', 'C', 'C', 'C', 'C', 'C'],
'Duration': [1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3],
'Yield': [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1],}
df = pd.DataFrame(raw_data, columns = ['Date', 'Portfolio', 'Duration', 'Yield'])
dft = df.groupby(['Date', 'Portfolio', 'Duration', 'Yield'], as_index =False)
這由對象創建一個熊貓集團。
然後我想其輸出到Excel中:
dft.to_excel(writer, 'test', index=False)
writer.save()
但是它返回一個錯誤:
AttributeError: Cannot access callable attribute 'to_excel' of 'DataFrameGroupBy' objects, try using the 'apply' method
爲什麼我需要申請嗎?我只希望按結果分組來刪除重複項。
嘗試'dft.apply(拉姆達X:x.to_excel(作家, '測試',指數= FALSE))' – EdChum
@EdChum這並不工作,它返回6行的投資組合「C」 – toasteez
什麼是你的本意嗎?一個'groupby'用於組上的聚合,爲什麼你不只是將索引設置爲那些列並輸出爲excel? – EdChum