如何在此数据框中打印每个资产(['资产A''资产B''资产C'])?我相信我的语法不正确。
import pandas as pd
df = pd.DataFrame({
'date': ['2019-01-01','2019-01-01','2019-01-01',
'2019-02-01','2019-02-01','2019-02-01',
'2019-03-01','2019-03-01','2019-03-01',
'2019-04-01','2019-04-01','2019-04-01',
'2019-05-01','2019-05-01','2019-05-01'],
'Asset': ['Asset A', 'Asset B', 'Asset C', 'Asset A', 'Asset B', 'Asset C',
'Asset A', 'Asset B', 'Asset C', 'Asset A', 'Asset B', 'Asset C',
'Asset A', 'Asset B', 'Asset C',],
'Monthly Value': [200, 800, 400, 400, 300, 600, 400, 400, 900,
300, 600, 400, 200, 100, 200],
'Indicator': [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0]
})
print(df.sort_values(by=['Asset']))
date Asset Monthly Value Indicator
0 2019-01-01 Asset A 200 0
3 2019-02-01 Asset A 400 0
6 2019-03-01 Asset A 400 0
9 2019-04-01 Asset A 300 0
12 2019-05-01 Asset A 200 1
1 2019-01-01 Asset B 800 0
4 2019-02-01 Asset B 300 0
7 2019-03-01 Asset B 400 1
10 2019-04-01 Asset B 600 0
13 2019-05-01 Asset B 100 0
2 2019-01-01 Asset C 400 0
5 2019-02-01 Asset C 600 0
8 2019-03-01 Asset C 900 0
11 2019-04-01 Asset C 400 1
14 2019-05-01 Asset C 200 0
此循环有问题
asset = df['Asset'].unique()
for asset in df['Asset']:
print(asset)
资产A,B和C的所需输出应如下所示。
date Asset Monthly Value Indicator
2 2019-01-01 Asset C 400 0
5 2019-02-01 Asset C 600 0
8 2019-03-01 Asset C 900 0
11 2019-04-01 Asset C 400 1
14 2019-05-01 Asset C 200 0
答案 0 :(得分:1)
应该使用unique
来代替groupby
。
for _, v in df.groupby('Asset'):
print(v, end='\n\n')
date Asset Monthly Value Indicator
0 2019-01-01 Asset A 200 0
3 2019-02-01 Asset A 400 0
6 2019-03-01 Asset A 400 0
9 2019-04-01 Asset A 300 0
12 2019-05-01 Asset A 200 1
date Asset Monthly Value Indicator
1 2019-01-01 Asset B 800 0
4 2019-02-01 Asset B 300 0
7 2019-03-01 Asset B 400 1
10 2019-04-01 Asset B 600 0
13 2019-05-01 Asset B 100 0
date Asset Monthly Value Indicator
2 2019-01-01 Asset C 400 0
5 2019-02-01 Asset C 600 0
8 2019-03-01 Asset C 900 0
11 2019-04-01 Asset C 400 1
14 2019-05-01 Asset C 200 0
如果您想使用unique
方法,那么这就是您要使用的方法
asset = df['Asset'].unique()
for a in asset:
print(df[df.Asset == a], end='\n\n')