random student
random student

Reputation: 775

DataError: No numeric types to aggregate when creating plot in loop

I want to make multiple line in lineplot with loop like this but it returns DataError: No numeric types to aggregate. Why it returns that error and how to fix this?

plt.figure()
cases = pd.DataFrame(data=covid[['date','acc_released','acc_deceased','acc_negative','acc_confirmed']])
for col in cases.columns:
    sns.lineplot(x=cases['date'],y=covid[col],data=cases)

Without loop it will be like this, which is not efficient but works fine

plt.figure()
sns.lineplot(x=covid['date'], y=covid['acc_confirmed'])
sns.lineplot(x=covid['date'], y=covid['acc_deceased'])
sns.lineplot(x=covid['date'], y=covid['acc_negative'])
sns.lineplot(x=covid['date'], y=covid['acc_released'])
plt.xticks(rotation=90)
plt.legend(['acc_confirmed', 'acc_deceased', 'acc_negative', 'acc_released'],
            loc='upper left')
plt.title('Numbers of cases')

This is my data

         date  acc_released  acc_deceased  acc_negative  acc_confirmed
0  2020-03-02             0             0           335              2
1  2020-03-03             0             0           337              2
2  2020-03-04             0             0           356              2
3  2020-03-05             0             0           371              2
4  2020-03-06             0             0           422              4
5  2020-03-07             0             0           422              4

It's supposed to look this way

enter image description here

Upvotes: 0

Views: 97

Answers (1)

matman9
matman9

Reputation: 490

If you set the date as your index you can pass the df to data;

sns.lineplot(data=cases)

to change the index;

df.index = df['Time']

then you can drop the time column;

df = df.drop(columns=['Time'])

Upvotes: 1

Related Questions