1

I have a data set like this

Data set

And I want to plot flux/date for each target in each passband Something like this

I have tried it using pandas but I have no idea how to do it. I have tried plot.bar and plot.hist

Greetings and thanks in advance

Edit: I'm using Modified Julian Date instead of Calendar date

1 Answer 1

1

Use pivot_table with DataFrame.plot, if necessary change aggregate function to sum, mean ...:

df = pd.DataFrame({'date':['2022-01-01','2022-01-10','2023-01-01', '2023-02-10'],
                   'passband':[1,1,2,2],
                   'flux':[2,8,5,6]})
print (df)
         date  passband  flux
0  2022-01-01         1     2
1  2022-01-10         1     8
2  2023-01-01         2     5
3  2023-02-10         2     6

df['date'] = pd.to_datetime(df['date'])

df['year'] = df['date'].dt.year
df1 = df.pivot_table(index='year',
                     columns='passband',
                     values='flux',
                     aggfunc='sum',
                     fill_value=0)
print (df1)
passband   1   2
year            
2022      10   0
2023       0  11

df1.plot()
Sign up to request clarification or add additional context in comments.

6 Comments

It gives me the next error: 'ValueError: Index contains duplicate entries, cannot reshape'
@A.Esquivias - Sorry, answer was changed to pivot_table
Should I use the whole data frame as data TypeError: pivot_table() missing 1 required positional argument: 'data'
@A.Esquivias - You are right, added solution with new helper column and sample data.
It worked out (no errors) but didn't plot it, just created a table instead of a graph
|

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.