我想为站的每个唯一值创建一个新的数据框。
我在下面尝试过,它只给了我在数据框中更新的最后一个站数据 = tai_new.i
tai['station'].unique() has 500 values.
for i in tai['station'].unique():
tai_new.i = tai[tai_2['station'] ==i]
Run Code Online (Sandbox Code Playgroud)
另一种方法是创建一个单独的列表
tai_stations = tai['station'].unique()
Run Code Online (Sandbox Code Playgroud)
然后创建两个循环,但是我不想输入 500 (IF) 条件。
您可以通过将对象转换为s 然后再转换为 来创建dictof :DataFramesgroupbytupledict
dfs = dict(tuple(tai.groupby('station')))
Run Code Online (Sandbox Code Playgroud)
样本:
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'>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
819 次 |
| 最近记录: |