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

Why does x[::-1] not reverse a grouped pandas dataframe column in Python 3.12.3?

  • Thread starter Thread starter compx
  • Start date Start date
C

compx

Guest
I have a large pandas DataFrame (160k records, 60 columns of mostly text) but for this example, I have the following DataFrame:

Code:
df1 = pd.DataFrame([{"GROUP": "A", "COL2": "1", "COL3": "P"},
                {"GROUP": "A", "COL2": "2", "COL3": "Q"},
                {"GROUP": "A", "COL2": "3", "COL3": "R"},
                {"GROUP": "B", "COL2": "4", "COL3": "S"},
                {"GROUP": "B", "COL2": "5", "COL3": "T"},
                {"GROUP": "B", "COL2": "6", "COL3": "U"},
                {"GROUP": "B", "COL2": "7", "COL3": "V"}])

I am trying to create another column that reverses the order of COL2 but only within groups of A and B...so I would expect from top to bottom, the values to be 3,2,1,7,6,5,4. This is achieved with this line of code:

Code:
df1['REVERSED_COL'] = df1.groupby("GROUP")["COL2"].transform(lambda x: x[::-1])

Instead, I get this: enter image description here

This line of code worked when I was using Python 3.11.7. However, I recently upgraded to 3.12.3 (and all other modules including pandas to 2.2.1) and removed 3.11.7 interpreter from my machine so I can't go back and test it again. I also have another machine using Python 3.7.4 where the same line of code still works as expected.

I tried using the x[::-1] on a list, and that reversed them as expected: This code:

Code:
x = [1,2,3,4,5,6,7,8,9]
print(x)
y = x[::-1]
print(y)

Results in:

Code:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]

My question is, am I doing something wrong? Is there a different way I can do this? I checked the pandas and python documentation on Python changes and pandas 2.2.1 changes (respectively) but I couldn't find anything relevant enough.
<p>I have a large pandas DataFrame (160k records, 60 columns of mostly text) but for this example, I have the following DataFrame:</p>
<pre><code>df1 = pd.DataFrame([{"GROUP": "A", "COL2": "1", "COL3": "P"},
{"GROUP": "A", "COL2": "2", "COL3": "Q"},
{"GROUP": "A", "COL2": "3", "COL3": "R"},
{"GROUP": "B", "COL2": "4", "COL3": "S"},
{"GROUP": "B", "COL2": "5", "COL3": "T"},
{"GROUP": "B", "COL2": "6", "COL3": "U"},
{"GROUP": "B", "COL2": "7", "COL3": "V"}])
</code></pre>
<p>I am trying to create another column that reverses the order of COL2 but only within groups of A and B...so I would expect from top to bottom, the values to be 3,2,1,7,6,5,4. This is achieved with this line of code:</p>
<pre><code>df1['REVERSED_COL'] = df1.groupby("GROUP")["COL2"].transform(lambda x: x[::-1])
</code></pre>
<p>Instead, I get this:
<a href="https://i.sstatic.net/Yjnd7Qux.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Yjnd7Qux.png" alt="enter image description here" /></a></p>
<p>This line of code worked when I was using Python 3.11.7. However, I recently upgraded to 3.12.3 (and all other modules including pandas to 2.2.1) and removed 3.11.7 interpreter from my machine so I can't go back and test it again. I also have another machine using Python 3.7.4 where the same line of code still works as expected.</p>
<p>I tried using the x[::-1] on a list, and that reversed them as expected:
This code:</p>
<pre><code>x = [1,2,3,4,5,6,7,8,9]
print(x)
y = x[::-1]
print(y)
</code></pre>
<p>Results in:</p>
<pre><code>[1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1]
</code></pre>
<p>My question is, am I doing something wrong? Is there a different way I can do this? I checked the pandas and python documentation on Python changes and pandas 2.2.1 changes (respectively) but I couldn't find anything relevant enough.</p>
 

Latest posts

Top