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.
You can create dict
of DataFrames
by convert groupby
object to tuple
s 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'>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With