3

Hello guys I have a program that takes two array "Year_Array" and "Month_Array" and generates the output according to conditions.

I want to add that both array in a single dataframe with column name year and name so in future I can add that dataframe with other dataframe.

Below is the sample code:

    Year_Array=[2010,2011,2012,2013,2014]
    Month_Array=['Jan','Feb','Mar','April','May','June','July','Aug','Sep','Oct','Nov','Dec']
    segment=[1, 1, 3, 5, 2, 1, 1, 1, 2, 1, 6, 1]
    p=0
    for p in range(0, len(Year_Array), 1):
        c=0
        for i in range(0, len(segment),1):
            h = segment[i]

            for j in range(0,int(h) , 1):         
                print((Year_Array[p]) ,(Month_Array[c]))

            c += 1

On the basis of segment the code is generated like this: output

2010 Jan
2010 Feb
2010 Mar
2010 Mar
2010 Mar
2010 April
2010 April
2010 April
2010 April
2010 April
2010 May
2010 May
2010 June
2010 July
2010 Aug
2010 Sep
2010 Sep
2010 Oct
2010 Nov
2010 Nov
2010 Nov
2010 Nov
2010 Nov
2010 Nov
2010 Dec

2011 Jan
2011 Feb
2011 Mar
2011 Mar
2011 Mar
2011 April
......
......

2012 Jan
2012 Feb
2012 Mar
2012 Mar
2012 Mar
2012 April
......
......so on till 2014

all this output i want to store in a single dataframe for this i tried with this way:

            df=pd.DataFrame(Year_Array[p])
            print(df)
            df.columns = ['year']
            print("df-",df)
            df1=pd.DataFrame(Month_Array[c])
            df1.columns = ['month']
            print(df1)

if i write :then this also print only the array values not the output

df=pd.DataFrame(Year_Array)
print(df)

**but this is not working i want the same ouput while printing the array in dataframe with column name "year" and "month"**please tell me how to do it..thanks

Sach
  • 904
  • 8
  • 20
snehil singh
  • 554
  • 1
  • 5
  • 18

1 Answers1

2

You can create a Array with the expected output and create a dataframe from it.

Edit : added column name to dataframe.

Year_Array=[2010,2011,2012,2013,2014]
Month_Array=['Jan','Feb','Mar','April','May','June','July','Aug','Sep','Oct','Nov','Dec']
final_Array=[]
segment=[1, 1, 3, 5, 2, 1, 1, 1, 2, 1, 6, 1]
p=0
for p in range(0, len(Year_Array), 1):
    c=0
    for i in range(0, len(segment),1):
        h = segment[i]
#             print(h)

        for j in range(0,int(h) , 1):         
            final_Array.append(((Year_Array[p], Month_Array[c])))

        c += 1
data = pd.DataFrame(final_Array,columns=['year','month'])
data.head()

Output :

  year month
0 2010 Jan
1 2010 Feb
2 2010 Mar
3 2010 Mar
4 2010 Mar
Sach
  • 904
  • 8
  • 20
  • sir please can u tell me how to remove that index column and a column name if possible – snehil singh Nov 28 '18 at 05:45
  • 1
    added column name, for index refer to https://stackoverflow.com/questions/20107570/removing-index-column-in-pandas – Sach Nov 28 '18 at 05:51