0

I have the following code:

import pandas as pd
import calendar
import datetime


dataframe_index = pd.date_range('2014-1','2014-12', freq='MS').strftime("%B")
data1 = pd.DataFrame(index =dataframe_index, columns = ['fruits'])


data1
Out[108]: 
          fruits
January      NaN
February     NaN
March        NaN
April        NaN
May          NaN
June         NaN
July         NaN
August       NaN
September    NaN
October      NaN
November     NaN
December     NaN

How can i add say apple to the month on July ? The index is not datetime format but string.

  • 1
    To assign values, I would check this out: https://stackoverflow.com/questions/13842088/set-value-for-particular-cell-in-pandas-dataframe-using-index. Might be useful – akb515 Jun 30 '20 at 18:06
  • 1
    you need `isin` `data1.loc[data1.index.isin(['July']),'fruits']= 'Apple'` already quite a few posts on this. I will mark as duplicate – Umar.H Jun 30 '20 at 18:08

1 Answers1

-1

You can use loc to capture July and redefine it as apple, like this

data1.loc['July'] = 'Apple'
Salman B
  • 154
  • 10
  • 1
    if there are multiple columns this will assign `Apple` to them all, try to use selective columns `data1.loc['July', 'Fruits'] = 'Apple'` – Umar.H Jun 30 '20 at 18:10
  • Why would you have multiple columns called 'July'? the question is only for one year – Salman B Jun 30 '20 at 18:20