python looping and creating new dataframe for each value of a column

家住魔仙堡 提交于 2020-12-05 11:52:25

问题


I want to create a new dataframe for each unique value of station.

I tried below which gives me only last station data updated in the dataframe = tai_new.i

tai['station'].unique() has 500 values.

for i in tai['station'].unique():
   tai_new.i = tai[tai_2['station'] ==i]

Another approach is creating a separate list of

tai_stations = tai['station'].unique()

And then create two loops however, I do not want to type 500 (IF) conditions.


回答1:


You can create dict of DataFrames by convert groupby object to tuples and then to dict:

dfs = dict(tuple(tai.groupby('station')))

Sample:

tai = pd.DataFrame({'A':list('abcdef'),
                   'B':[4,5,4,5,5,4],
                   'C':[7,8,9,4,2,3],
                   'D':[1,3,5,7,1,0],
                   'E':[5,3,6,9,2,4],
                   'station':list('aabbcc')})

print (tai)
   A  B  C  D  E station
0  a  4  7  1  5       a
1  b  5  8  3  3       a
2  c  4  9  5  6       b
3  d  5  4  7  9       b
4  e  5  2  1  2       c
5  f  4  3  0  4       c

dfs = dict(tuple(tai.groupby('station')))

#select each DataFrame by key - name of station
print (dfs['a'])
   A  B  C  D  E station
0  a  4  7  1  5       a
1  b  5  8  3  3       a

print (type(dfs['a']))
<class 'pandas.core.frame.DataFrame'>



回答2:


Please use this

for i in tai['station'].unique(): tai_new[i] = tai[tai_2['station'] ==i]

assuming tai_new is a dict.



来源:https://stackoverflow.com/questions/46476310/python-looping-and-creating-new-dataframe-for-each-value-of-a-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!