say you have

    for user_id, group in df.sort_values(
          ['user_id', 'timestamp'], ascending=[True, False]
         group = group.reset_index()
         positive = None
         for index, row in group.iterrows():
             # print(index)
             if row.rating_type not in [20, 90]:
                 positive = row

                 num_prior = self.num_prior
                 low = max(0, index - num_prior)
                 priors = group.drop_duplicates(subset=['user_id', 'review_meta_id'])[low:index]

                 result_positive_dict = {
                     'user_id': user_id,
                     'review_meta_id': positive.review_meta_id,
                     'prior_ids': ','.join(priors.review_meta_id),
                     'target': 1
                 # 20, 90 = negative
                 positives = group[(group.index>=index) & (~group.rating_type.isin([20, 90]))][:10]

                 num_negative = 4

                 for i in range(num_negative):
                     index_sample = random.sample(range(index+1), 1)[0]
                     sample = group.iloc[index_sample]

                     low = max(0, index_sample - num_prior)

                     try_count = 5
                     for _ in range(try_count):
                         if sample.rating_type not in [20, 90] or sample.review_meta_id in positives.review_meta_id:
                             index_sample = random.sample(range(index+1), 1)[0]


after few more lines, your screen doesn't show the first line for user_id, group in df.sort_values( which has the right indent level you want..

How do you place your cursor at the right indentation level in such cases?

1 Answer 1


For Python, I recommend elpy-mode. It has vertical guides for this, by default.


There is also python-mode (required by elpy-mode) which automatically cycles through the various indentation levels using <TAB>. If it gets too confusing to track indentations, I would consider that a code smell. Maybe think about refactoring.

There are also a slew of options, all falling into the category of "put the code you want on the screen".

These include:

  1. Recentering with C-l (recenter-top-bottom)
  2. Opening a new window with C-x 2 (split-window-below) or C-x 3 (split-window-right) and visit the file in each window. The code in each window can be "out of sync" with one another.
  3. Make the text smaller with C-x C-- (text-scale-adjust)
  4. Fold the code with hs-mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.