0

How do I print each asset in this dataframe (['Asset A' 'Asset B' 'Asset C'])? I believe the my syntax is incorrect.

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

Something is wrong with this loop

asset = df['Asset'].unique()
for asset in df['Asset']:
    print(asset)

Desired output should look like this for Assets A, B, and 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

1 Answer 1

1

Instead of using unique you should use 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

If you want to use the unique method then this is the way you would do it

asset = df['Asset'].unique()
for a in asset:
    print(df[df.Asset == a], end='\n\n')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.