1

The problem: I want to shift the index by adding 2 (index+=2) to match it with the indices of a .csv file.

I have a dataframe which looks as follows:

Counts Counts
7 10951.0 870.0
8 8653.0 1772.0
19 28598.0 37608.0
20 115266.0 123491.0
21 73097.0 145261.0

my desired result would look like this:

Counts Counts
9 10951.0 870.0
10 8653.0 1772.0
21 28598.0 37608.0
22 115266.0 123491.0
23 73097.0 145261.0

Thanks for the help and sorry if there is anything wrong with my question.

This function:

shifted_df.index = pd.Index(range(2, len(shifted_df) + 2))

is the first one which as actually changing the index of my dataframe but it just overwrites the given index with the numbers 2 to len(shifted_df)

mozway
  • 194,879
  • 13
  • 39
  • 75

1 Answers1

0

If you want to modify your shifted_df DataFrame in place:

shifted_df.index += 2

If you want to create a new DataFrame, and assuming df the original DataFrame use set_axis:

shifted_df = df.set_axis(df.index+2)

Or rename:

shifted_df = df.rename(lambda x: x+2)

Output:

          col1      col2
9      10951.0     870.0
10      8653.0    1772.0
21     28598.0   37608.0
22    115266.0  123491.0
25      1599.0    4536.0
27     61142.0   46921.0
...
1302  117695.0  108044.0
mozway
  • 194,879
  • 13
  • 39
  • 75
  • thanks for the help but I get a TypeError: Exception has occurred: TypeError int() argument must be a string, a bytes-like object or a real number, not 'Index'. The Type of the index seems to be: so I can't add 2 – philiVanilli25 Apr 14 '23 at 11:59
  • please provide the output of `df.head().to_dict()` assuming `df` your DataFrame – mozway Apr 14 '23 at 12:01
  • >>print(added_region_names.head().to_dict()) {'RegionCellCount': {7: 729.1193181818181, 8: 1485.056818181818, 19: 31518.06818181818, 20: 103493.87784090909, 21: 121738.6221590909}} – philiVanilli25 Apr 14 '23 at 12:31
  • @philiVanilli25 this small sample show correct numbers. What about `added_region_names.index[pd.to_numeric(added_region_names.index, errors='coerce').isna()]`? – mozway Apr 14 '23 at 12:33
  • Its still the same output, but thanks for the help ! – philiVanilli25 Apr 14 '23 at 12:41
  • This wasn't a fix but a question to debug. What is the output of `added_region_names.index[pd.to_numeric(added_region_names.index, errors='coerce').isna()]`? If this gives an empty Index then I don't see where your issue could come from. If this gives non numeric data then we'll see what we can do – mozway Apr 14 '23 at 12:42
  • I tried the lambda function you provided as well: Exception has occurred: TypeError can only concatenate str (not "int") to str. This is the error I got – philiVanilli25 Apr 14 '23 at 12:43
  • Index(['RegionCellCount'], dtype='object') thats the output of: added_region_names.index[pd.to_numeric(added_region_names.index, errors='coerce').isna()] – philiVanilli25 Apr 14 '23 at 12:45
  • I refactored the code and applyed the first functioned you mentioned above and it worked. Thanks for the help ! – philiVanilli25 Apr 21 '23 at 13:51