0
Good afternoon guys, I have the following situation. I have a spreadsheet with the following columns: name, surname, dates(from 2011 until 2021)
Follow the table to view:
As you can see, these dates are out of order. How can I sort them without changing the columns name and surname?
I came to execute the code in the following way:
df_json_meses = df_json1.iloc[:, 58:64] # Coluna referente aos meses de ago até dez/2020
df_json_meses2 = df_json1.iloc[:, 52:55] # Coluna que se refere aos meses jan a maio/2021
df_json_meses3 = df_json1.iloc[:, 99] # coluna referente ao mês de jun/2021
df_json_meses4 = df_json1.iloc[:, 55:57] # coluna referente aos meses Jul e Ago/2021
df_json_soma_meses = pd.concat([df_json_meses, df_json_meses2 , df_json_meses3,
df_json_meses4], Axis=1)
df_json_nome = df_json1.iloc[:, :2]
df_json_unico = pd.concat([df_json_nome, df_json_soma_meses], axis=1)
I managed to organize as I wanted, only that I answer me for this report, now if I generate a new report, and generate new columns or fewer columns and the amount is smaller or greater than I have set in the code, I have to change in hand? If so, I don’t want it. How can I automate this code in the best way possible?
Follow the final result of the report:
I really appreciate anyone who can help.
Good morning jfaccioni. Thanks for the help.. I did it. I didn’t need to use df.pop and df.Insert to remove and insert the column at the beginning. I did so: df.sort_index (Axis = 1, inplace = True, ascending = False) and it worked.
– Fabricio Rezende
Another question jfaccioni, sorry to bother you. I want to pick up a column-specific Qtde from 2021-08 until 2020-09. I made my code like this below: df_names = df.iloc[:, :2] df_months = df.iloc((:, 2:14) - 2:14 Qtde of date columns I want. df_unico = pd.Concat([df_names, df_months], Axis = 1)

– Fabricio Rezende
Another question jfaccioni, sorry to bother you. I want to pick up a column-specific Qtde from 2021-08 until 2020-09. I made my code like this below: df_names = df.iloc[:, :2] df_months = df.iloc((:, 2:14) - 2:14 Qtde of date columns I want. df_unico = pd.Concat([df_names, df_months], Axis = 1) df_unico.to_excel('test.xlsx', index = False) My question is whether, for example, I generate a new report in which I increase or decrease the number of date columns, this code I made will work smoothly or I will have to change in hand?
– Fabricio Rezende
Hi @Fabriciorezende, the simplest way I can think is to use the column name (method
.loc
) instead of its position (method.iloc
which you are using). For example, start fromcols = ['2021-08', '2021-07', ... , '2020-10', 2020-09']
and then write downdf.loc[:, cols]
. Your problem becomes "how to create a list of all months between a final date'2021-08'
and an initial date'2020-09'
". I suggest searching for the answer on this and other websites, or opening a new question if you don’t find it.– jfaccioni
About your call
df.sort_index(axis=1, inplace=True, ascending=False)
, should work yes, because theascending=False
sorts the columns in reverse order that I demonstrated in my reply. Just note that this causes the column'Sobrenome'
appear before'Nome'
, but if that’s not a problem for you, then you really don’t need to usedf.pop
anddf.insert
.– jfaccioni
Gratitude @jfaccioni. I managed to make it work. I gave a Sort.index on my dataframe df_months. It was the way I need it. Vlw same... Hugs.
– Fabricio Rezende