tai['station'].unique() has 500 values.
for i in tai['station'].unique():
tai_new.i = tai[tai_2['station'] ==i]
另一种方法是创建一个单独的列表
tai_stations = tai['station'].unique()
接下来需要创建两个循环,但是我不想打500个(IF)条件语句。
tai['station'].unique() has 500 values.
for i in tai['station'].unique():
tai_new.i = tai[tai_2['station'] ==i]
另一种方法是创建一个单独的列表
tai_stations = tai['station'].unique()
接下来需要创建两个循环,但是我不想打500个(IF)条件语句。
groupby
对象转换为元组,然后再转换为 dict
,来创建 DataFrames
的字典形式:
dfs = dict(tuple(tai.groupby('station')))
示例:
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'>
for i in tai['station'].unique(): tai_new[i] = tai[tai_2['station'] ==i]
假设tai_new是一个字典。