OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Sorting Groups by the Order they are in Excel

  • Thread starter Thread starter Nyanga
  • Start date Start date
N

Nyanga

Guest
I basically want my before_X.groupy and after_X.groupby groups to be sorted by which ever str[:3] comes first in the excel sheet according to the row. Right now the groups that are further down are being printed first. I have a snippet of the code here:

Code:
before_groups = before_X.groupby(before_X.iloc[:, 7].str[:3])
after_groups = after_X.groupby(after_X.iloc[:, 7].str[:3])


    for group_name, group in before_groups:
        mail_body += f"{group_name} RB –\n"
        for index, row in group.iterrows():
            item = row.iloc[2]  # Assuming column C contains the items
            if pd.notna(item):
                mail_body += str(item) + '\n'
        mail_body += '\n'

    for group_name, group in after_groups:
        mail_body += f"{group_name} HO –\n"
        for index, row in group.iterrows():
            item = row.iloc[2]  # Assuming column C contains the items
            if pd.notna(item):
                mail_body += str(item) + '\n'
        mail_body += '\n'

Look above ^^^. I want the groups that are grouped by the first three letters of a column are printed in the order that they appear in the excel by row.
<p>I basically want my before_X.groupy and after_X.groupby groups to be sorted by which ever str[:3] comes first in the excel sheet according to the row. Right now the groups that are further down are being printed first. I have a snippet of the code here:</p>
<pre><code>before_groups = before_X.groupby(before_X.iloc[:, 7].str[:3])
after_groups = after_X.groupby(after_X.iloc[:, 7].str[:3])


for group_name, group in before_groups:
mail_body += f"{group_name} RB –\n"
for index, row in group.iterrows():
item = row.iloc[2] # Assuming column C contains the items
if pd.notna(item):
mail_body += str(item) + '\n'
mail_body += '\n'

for group_name, group in after_groups:
mail_body += f"{group_name} HO –\n"
for index, row in group.iterrows():
item = row.iloc[2] # Assuming column C contains the items
if pd.notna(item):
mail_body += str(item) + '\n'
mail_body += '\n'
</code></pre>
<p>Look above ^^^. I want the groups that are grouped by the first three letters of a column are printed in the order that they appear in the excel by row.</p>
 
Top