0

I have a dataset:

          A     B      C    D    yearweek
    0    245    95    60    30   2014-48
    1    245    15    70    25   2014-49
    2    150   275   385   175   2014-50
    3    100   260   170   335   2014-51
    4    580   925   535  2590   2015-02
    5    630   126   485  2115   2015-03
    6    425    90   905  1085   2015-04
    7    210   670   655   945   2015-05

How to plot each value against 'yearweek'?

I tried for example:

    import matplotlib.pyplot as plt
    import pandas as pd

    new = pd.DataFrame([df['A'].values, df['yearweek'].values])
    plt.plot(new)

but it doesn't work and shows

    ValueError: could not convert string to float: '2014-48'

Then I tried this:

    plt.scatter(df['Total'], df['yearweek'])

turns out:

    ValueError: could not convert string to float: '2015-37'

Is this means the type of yearweek has some problem? How can I fix it?

Or if it's possible to change the index into date?

1
  • what is your expected result? can you provide an example? and what "dataset" is used? is that a standard class? Commented Oct 25, 2017 at 11:22

2 Answers 2

1

The best solution I see is to calculate the date from scratch and add it to a new column as a datetime. Then you can plot it easily.

df['date'] = df['yearweek'].map(lambda x: datetime.datetime.strptime(x,"%Y-%W")+datetime.timedelta(days=7*(int(x.split('-')[1])-1)))

df.plot('date','A')

So I start with the first january of the current year and go forward 7*(week-1) days, then generate the date from it.

Sign up to request clarification or add additional context in comments.

5 Comments

Usually you parse datetime with pandas.to_datetime, with a format string. Unfortunatelly "%Y-%W" or "%Y-%U" always return the first of January of the given year
Before I do want to calculate it but failed. =p This seems a good solution, thank you as well!
I tried this way, but there is an error: {ValueError: time data '2014-W48' does not match format '%Y-%W'}. I think it's because I saved year and week together as {year + '-W' + week}. Will it work if I move the '-W' part?
@Jenny, then you should adjust the format to match your data. If you have something like "2014-W48", then use "%Y-W%W" as a format.
For information, the above mentionned solution of Vivek does NOT work as expect. It just plot the x-axis as every week in a row. If you have missing weeks or jumps in the years or data entered in the wrong order, this will fail. Just try with yearweek=['2014-49','2014-48','2016-50'] (in this order)
1

As of pandas 0.20.X, you can use DataFrame.plot() to generate your required plots. It uses matplotlib under the hood -

import pandas as pd
data = pd.read_csv('Your_Dataset.csv')

data.plot(['yearweek'], ['A'])

Here, yearweek will become the x-axis and A will become the y. Since it's a list, you can use multiple in both cases

Note: If it still doesn't look good then you could go towards parsing the yearweek column correctly into dateformat and try again.

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.