'Having an issue plotting: Columns must be same length as key
I'm new to Python and I'm trying to adjust this code to my data:
import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
# Create fake data
cols=['DATE','TAVG']
numdays = 92
date_list = pd.date_range(start='1/1/2022', periods=numdays, freq='D').astype('str').tolist()
tavg_list = [round(random.uniform(-3.33, 3.33), 2) for i in range(numdays)]
df = pd.DataFrame({"DATE": date_list, "TAVG": tavg_list})
# line plot based on moving average
ma_days = 10 # number of days for moving average
# use seaborn styling on plot
sns.set()
sns.set_context("notebook") # options: paper, notebook, talk, poster
sns.set_style("darkgrid", {"axes.facecolor":"0.9",'font.family':['Roboto']})
# tuple with size of plot (W,H)
pltsize = (10,7)
# plot bars from the dataframe
ax = df.plot(x='DATE', y='TAVG', kind='bar', legend=False, figsize=pltsize,
color=(df['TAVG'] > 0).map({True:'darkorange', False:'royalblue'}))
# calc moving average and save to df.
df['MA'] = df.rolling(window=ma_days).mean()
# plot line chart on same bar chart
df.plot(ax=ax, x='DATE', y='MA', kind='line', legend=False, figsize=pltsize,
color='black', lw=1)
# change x-axis labels to only so monthly
ax.xaxis.set_major_locator(mdates.MonthLocator())
plt.show()
I have this dataframe:
DATE TAVG
0 1955-06-01 NaN
1 1955-06-02 NaN
2 1955-06-03 NaN
3 1955-06-04 NaN
4 1955-06-05 NaN
... ... ...
5805 2020-08-27 2.067854
5806 2020-08-28 3.267854
5807 2020-08-29 3.067854
5808 2020-08-30 1.567854
5809 2020-08-31 4.167854
The name of it is avgsum, I want to construct a graph like this.
I want a line with the 10 years rolling average and the x axis to display only decades.
When I try writing the code, i always get some kind of error. Like this:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_5361/1371129499.py in <module>
23
24 # calc moving average and save to df.
---> 25 avgsum['MA'] = avgsum.rolling(window=ma_days).mean()
26
27 # plot line chart on same bar chart
~/Downloads/enter/lib/python3.9/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
3643 self._setitem_array(key, value)
3644 elif isinstance(value, DataFrame):
-> 3645 self._set_item_frame_value(key, value)
3646 elif (
3647 is_list_like(value)
~/Downloads/enter/lib/python3.9/site-packages/pandas/core/frame.py in _set_item_frame_value(self, key, value)
3773 len_cols = 1 if is_scalar(cols) else len(cols)
3774 if len_cols != len(value.columns):
-> 3775 raise ValueError("Columns must be same length as key")
3776
3777 # align right-hand-side columns if self.columns
ValueError: Columns must be same length as key
What am I doing wrong? What should I do? Any help would be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
