0

I have below tabular data and I want to convert to format at bottom so that i can load it into my database.

How can i achieve it by using Panda?

I am using Python but excel method also welcome

enter image description here

Chelseajcole
  • 487
  • 1
  • 9
  • 16
  • Can you copy-paste your data, instead of the image? It would easier for other people to Simpy use that data instead of the data shown in the image. – harvpan May 31 '18 at 03:30
  • 2
    You are talking about [`DataFrame.melt()`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.melt.html), e.g.: `df.melt(id_vars='Date', value_vars=['A', 'B', 'C', 'D'], value_name='Number')` – AChampion May 31 '18 at 03:33

2 Answers2

0

You need:

data.set_index(['date']).stack(dropna=False).reset_index(name='Number').rename(columns={'level_1':'Item'})

Output:

   date Item    Number
0   1   A   NaN
1   1   B   12.0
2   1   C   10.0
3   1   D   NaN
4   2   A   10.0
5   2   B   30.0
6   2   C   NaN
7   2   D   NaN
8   3   A   NaN
9   3   B   NaN
10  3   C   NaN
11  3   D   90.0
harvpan
  • 8,571
  • 2
  • 18
  • 36
0

Use df.fillna() to fill the blank with NULL value, followed by df.melt() and then followed with df.set_index(). At the end, rename the column to columns name you desired with df.rename() or more details in this topic

In []: df
Out[]: 
   Date     A     B    C     D
0     1   NaN  54.0  435   NaN
1     2  23.0   3.0  345   NaN
2     3  43.0   4.0   78   NaN
3     4   4.0   NaN  423   NaN
4     5  34.0  54.0    4  45.0
5     6   NaN  54.0    3  87.0

In []: (
  ...: df.fillna('NULL')
  ...:     .melt(id_vars='Date', value_vars=['A','B','C','D'])
  ...:     .set_index('Date')
  ...:     .rename(columns={'variable':'Item', 'value':'Number'}))
  ...: )

Out[]: 
     Item Number
Date            
1       A   NULL
2       A     23
3       A     43
4       A      4
5       A     34
6       A   NULL
1       B     54
2       B      3
3       B      4
4       B   NULL
5       B     54
6       B     54
1       C    435
2       C    345
3       C     78
4       C    423
5       C      4
6       C      3
1       D   NULL
2       D   NULL
3       D   NULL
4       D   NULL
5       D     45
6       D     87
Toto Lele
  • 394
  • 2
  • 13