2014-03-28 19 views

回答

0

該解決方案對我來說很不錯。 另一種可能是使用列表解析生成名稱,數據幀的元組,然後您可以遍歷並保存:

import pandas as pd 
import random 

#Create some random data 
DF = pd.DataFrame({'A': [random.randint(1, 10) for x in xrange(200)], \ 
        'Data' : [random.random() for x in xrange(200)]}) 

#Create list of tuples where first element in each tuple is the name of the group 
#and the second element is a data frame containing the data relating to that group. 

segments = [(group[0], pd.DataFrame(group[1])) for group in DF.groupby('A', sort = False)] 

然後,您可以遍歷鏈段保存,或瀏覽他們,或任何你喜歡真的......列表理解爲您節省了一些代碼行,但我不確定它的一定會比您所做的更好。

相關問題