0

I have a dataframe that looks like below

enter image description here

I would like to convert this to following dataframe by adding headers and also put the value on last column as header as follows

enter image description here

Any help on this would be appreciated, i tried .groupby but it requires a column name which i dont have now.

windowws
  • 373
  • 1
  • 8
  • 20

3 Answers3

4

You can use pandas.pivot_table

import numpy as np 
import pandas as pd

# ... your data in df

df = pd.read_csv(pd.compat.StringIO('''date value data
01/01/2019 30 data1
01/01/2019 40 data2
02/01/2019 20 data1
02/01/2019 10 data2'''), sep=' ')

results = pd.pivot_table(df, values='value', index=['date'],
                     columns=['data'], aggfunc=np.sum, fill_value=0)

print(results)

Results: enter image description here

Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
0
df=pd.DataFrame({'X':['01/01/2009','01/01/2009','02/01/2009','02/01/2009'],
                'Y':[20,30,40,25],
                'data':['Data1','Data2','Data1','Data2']})
df.pivot(index='X',columns='data',values='Y')
df.columns = ['Date' , 'Data1' , 'Data2']
df
code snippet

enter image description here

vrana95
  • 511
  • 2
  • 10
0

Alternative method using DataFrame.stack

import pandas as pd


data = {
    0: ['01-01-2009', '01-01-2009', '02-01-2009', '02-01-2009'],
    1: [20, 30, 40, 25],
    2: ['Data1', 'Data2', 'Data1', 'Data2']
}
df = pd.DataFrame(data)
renamed = df.rename(columns={0: 'date', 1: 'values', 2: 'source'})
new_index = renamed.set_index(['date', 'source'])
result = new_index.unstack(1)['values']
Meow
  • 1,207
  • 15
  • 23