Reputation: 31
I have the following: I imported an excel list called example with the date in the form day-month-year, which is the index, and then I have in a second column the corresponding volumes.
With python I extracted the month and the year.
example['Year']= example.index.year
example['month']= example.index.month
Now I want to get the days per month
I tried:
x=monthrange(example['Year'], example'[month'])
Does not work, the result is ambiguous.
for i in example['year']:
for j in eaxmaple['month']:
x=monthrange(i,j)
does not work either, since it does not combine the year and month, it just loops through all year, and then trhough all months.
How do I solve this? I would like to add a colum with the days per month, so I have one colum with the date, one column with the year, one column with the month and one with the days per month
Upvotes: 0
Views: 389
Reputation: 1805
Does this work? :
x=[monthrange(i, j) for (i, j) in zip(example['Year'], example['month'])]
Or if you want only the days in month:
x=[monthrange(i, j)[1] for (i, j) in zip(example['Year'], example['month'])]
Or with pandas:
example['days_in_month'] = example.apply(lambda df: monthrange(df['Year'], df['month']))
Upvotes: 1