0

I have a wide dataframe like this in Python Pandas and I need to reshape it to the long format:

import pandas as pd
import random

df = pd.DataFrame(columns = ['application', 'a','b','c','d', 'e','f', 'shortlisted'])
df.application = list(range(10))

randomList = []
# Set a length of the list to 10
for i in range(0, 10):
    # any random numbers from 0 to 1000
    randomList.append(random.randint(0, 1))
    
randomDecimal = []
for i in range(0, 100):
    # any random numbers from 0 to 1000
    randomDecimal.append(round(random.uniform(0, 1),2))
    
df['a'] = randomDecimal[0:10]
df['b'] = randomDecimal[10:20]
df['c'] = randomDecimal[20:30]
df['d'] = randomDecimal[30:40]
df['e'] = randomDecimal[40:50]
df['f'] = randomDecimal[50:60]

df.shortlisted = randomList
df

enter image description here

And here is the expected output:

# expected output: a long dataset
# application measure value shortlisted
# 0           a       0.35  1
# 0           b       0.42  1
# ...

What should I do?

Hamideh
  • 665
  • 2
  • 8
  • 20

0 Answers0