0

I am reading csv file with large number of rows and columns. The csv file looks like output of below code.


import pandas as pd
df = pd.DataFrame({'date': ['2016-11-24', '2016-11-25', '2016-11-26'], 
                   'us': [40000, 50000, 42000],
                   'uk':[30000, 70000, 82000]})
print(df)

#output

         date     us     uk
0  2016-11-24  40000  30000
1  2016-11-25  50000  70000
2  2016-11-26  42000  82000

However, I want to reframe the dataframe in the form given below:

df2 =pd.DataFrame({'date': ['2016-11-24', '2016-11-24', '2016-11-25','2016-11-25', '2016-11-26', '2016-11-26'], 
                   'country': ['us','us','us','uk','uk','uk'],
                   'value':[40000, 50000, 42000,30000, 70000, 82000]})
print(df2)

output2:

         date country  value
0  2016-11-24      us  40000
1  2016-11-24      us  50000
2  2016-11-25      us  42000
3  2016-11-25      uk  30000
4  2016-11-26      uk  70000
5  2016-11-26      uk  82000

Is there any better way to implement this? The actual dataframe shape is 42000x64.

srinivas
  • 301
  • 1
  • 9
  • 2
    they call this melting. it has [documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.melt.html) for the explainatiation. `df.melt(id_vars="date", var_name="country")` –  Feb 13 '22 at 13:36
  • I didnot know about melt. It worked. – srinivas Feb 14 '22 at 04:55

0 Answers0