0

I have a code which works well with individual column names:

df

Date         Biscuits  Ice cream   Candies   Honey  year  month
2017-12-1    12         23          44        3     2017   Dec
2019-11-1    11         20          10        4     2019   Nov
2018-10-1    4          11           NAN      2     2018   Oct

I wish to plot Biscuits, Ice creams, candies and say honey. The below code works fine

import matplotlib.pyplot as plt
from matplotlib import dates as mdates
# Plot the data
fig, ax = plt.subplots(figsize=(10, 2))
for col in ['Biscuits','Ice Cream','Candies','Honey']:
    ax.plot(df['Date'], df[col], label=col)
years = mdates.YearLocator()    # only print label for the years
months = mdates.MonthLocator()  # mark months as ticks
years_fmt = mdates.DateFormatter('%Y')
ax.xaxis.set_major_locator(years)
ax.xaxis.set_minor_locator(months)
ax.xaxis.set_major_formatter(years_fmt)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

For the same code, I wanted to use all the columns except few columns without specifying the column names separately like biscuits, honey etc

import matplotlib.pyplot as plt
from matplotlib import dates as mdates
# Plot the data
fig, ax = plt.subplots(figsize=(10, 2))
arr=df.columns.value_counts().drop(['year'],['Date'],['month']).index #this is where we need all columns except few columns
for col in arr:
    ax.plot(df['Date'], df[col], label=col)
years = mdates.YearLocator()    # only print label for the years
months = mdates.MonthLocator()  # mark months as ticks
years_fmt = mdates.DateFormatter('%Y')
ax.xaxis.set_major_locator(years)
ax.xaxis.set_minor_locator(months)
ax.xaxis.set_major_formatter(years_fmt)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))

It is not working. How to have all the columns instead of custom column names only.

EDIT:(Not part of original question which has been answered below):

Just one more thing, lets say apart from dropping few columns, I want to include only custom columns, say column 1, 3 and 4 in this case(need a generic solution)(i.e. Biscuits , Candies and Honey) using column position, can anyone add to answer in that case?

7
  • "It is not working". How is it not working? What error do you get? Commented Jan 30, 2020 at 16:01
  • .drop(['year','Date','month'])? Commented Jan 30, 2020 at 16:02
  • @KenHBS: empty plot with nothing' Commented Jan 30, 2020 at 16:05
  • 1
    @Andrea .drop(columns=[...]) otherwise it will try to remove rows (=indexes) Commented Jan 30, 2020 at 16:06
  • @DeepSpace You are right indeed Commented Jan 30, 2020 at 16:09

1 Answer 1

5

I would solve it defining arr without the columns you don't want and later use the for loop:

arr=df.drop(columns=['year','Date','month']) #this is where we need all columns except few columns
for col in arr:
    ax.plot(df['Date'], df[col], label=col)
Sign up to request clarification or add additional context in comments.

4 Comments

list(arr) is redundant?
It certainly is.
@CeliusStingher Thanks . Just tell me one thing what if lets say i wnt to drop year, date and month and include columns starting from position 1 to n, say 1 to 3 in this case (need a generic solution)
@ShailajaGuptaKapoor you could use iloc i.e. df.drop(['year','Date','month']).iloc[:, :n]

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.