0

I have a dataframe and i need to segment the information by variables, for example the dataframe has 2400 rows x 9 columns records and I want to have 24 variables of 100 records from column 2, and the same with others columns

I also need my variables to be 201801_col2 201802_col2...

(2018 is the year of record and 01,02,03.. is the month)

i did two methods but i could not make it


1º method

for i in range(2018,2019):
    for j in range(1,13):
        if j<10:
            j='0'+str(j)
            str(i)+str(j)+'_col2'=col2[:j*100]

with the method i was closer, because i have the 24 variables with 100 records, but i could not have the name of de variables 2º method

terms = [] #empty list
for i in range(2016,2019):
    for j in range(1,13):
        terms.append(j)
        a=(j-1)*100
        b=(j)*100
        terms[j-1]=salario.iloc[a:b,]
   
toyota Supra
  • 3,181
  • 4
  • 15
  • 19
  • 1
    Does this answer your question? [Pandas long to wide reshape, by two variables](https://stackoverflow.com/questions/22798934/pandas-long-to-wide-reshape-by-two-variables) – Stuart Jun 25 '23 at 09:31
  • You probably need `pivot` or similar. This is called 'reshaping data from long to wide' – Stuart Jun 25 '23 at 09:32

0 Answers0