0

I'm amazed, maybe someone can explain what's happening....

When I run this very simple example:

df = pd.DataFrame(columns=['A','B','C'])
results = pd.DataFrame(columns=df.columns)
for i, col in enumerate(df):
   print('.....'+col)
   result = [print(col) for i in range(2)]  

The result is (col is unknown the 1st time):

.....A
A
A
.....B
A
A
.....C
A
A

But what I really expected is:

.....A
A
A
.....B
B
B
.....C
C
C

What is happening??

Izaskun
  • 173
  • 1
  • 8
  • I test it and get expected output in pandas 0.23.0 – jezrael Jun 14 '18 at 11:27
  • The short answer is don't user `print` in a list comprehension. See marked duplicate for more details. – jpp Jun 14 '18 at 11:28
  • Works as expected for me – Conradin Jun 14 '18 at 11:29
  • @jpp I disagree with the duplicate. This is probably an issue related to Python 2's list comprehensions leaking variables. – ayhan Jun 14 '18 at 11:30
  • @user2285236, Yet the post is marked `[python-3.x]`.. Nevertheless, feel free to reopen if you feel this behaviour deserves an explanation. – jpp Jun 14 '18 at 11:31
  • @jpp How does printing inside a comprehension explain `col` variable having the same value all the time? – ayhan Jun 14 '18 at 11:34
  • @user2285236, I added a few dups for `[python-2.x]` variable leaking. If you think you can add more value, feel free to reopen. – jpp Jun 14 '18 at 11:38
  • @jpp I wasn't able to reproduce this it might have been an incorrect guess. I don't see any point in reopening just to vote to close it with another reason so it is best to wait for the op to explain I guess. Thanks. – ayhan Jun 14 '18 at 11:44
  • It seems to be a problem with my IDE, PyCharm..... If I run it in the console it works, but not in PyCharms. Thank you `code` Python 3.6.1 |Anaconda custom (64-bit)| (May 11 2017, 13:09:58) import pandas as pd df = pd.DataFrame(columns=['A','B','C']) results = pd.DataFrame(columns=df.columns) for i, col in enumerate(df): print('.....'+col) result = [print(col) for i in range(2)] .....A Traceback (most recent call last): NameError: name 'col' is not defined`code` – Izaskun Jun 14 '18 at 11:57

1 Answers1

0

I just ran:

import pandas as pd

df = pd.DataFrame(columns=['A','B','C'])
results = pd.DataFrame(columns=df.columns)
for i, col in enumerate(df):
   print('.....'+col)
   result = [print(col) for i in range(2)] 

and it returned:

Out[]:
..A
A
A
..B
B
B
..C
C
C

Python 3.6.5
Pandas 0.20.3

Dillon
  • 997
  • 4
  • 13
  • It seems to be a problem with my IDE, PyCharm..... If I run it in the console it works, but not in PyCharms. Thank you – Izaskun Jun 14 '18 at 11:56