[Python][Pandas][ML study notes 04] Sort/filter data by attribute value of (undecided) column and save as csv file

>Problem description

In Pandas, when I want to group an original dataset by the value of a certain column, and save the grouped data as a DataFrame (which can be further saved as a CSV file or others), if the column (group) has The number of attribute values ​​is undetermined, how to operate?

> problem solving

You need to use pandas' groupby method:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html

col_id = 'your_col_id'
origin_data = pd.read_csv('./data/your_data.csv')
gp = origin_data.groupby(col_id)
index_list = list(gp.describe().index)
for i in index_list:
    print 'now at group:', i
    origin_data.loc[origin_data[col_id] == i].to_csv('./data/divided/divided_data_'+str(i)+'.csv', index=False)


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325644659&siteId=291194637