Number Logic
Number Logic

Reputation: 894

data frames with a for loop

I am relatively new to Python and came across an interesting piece of code that I am trying to understand in greater detail. My data frame looks something like this:

import pandas as pd
d = {'business': ['FX','a','CR'], 'A/L': ['A','A','L']}
data = pd.DataFrame(data=d)
data

Someone very kindly posted the following piece of code on this site:

cols=data.columns
pd.DataFrame({col: data[col].str.contains('#') for col in cols})

I understand what the code inside the pd.DataFrame is doing. However, I cannot find any documentation on how the for loop works in the above context. This does not look like a list comprehension even though it works something like a list comprehension.

Can someone point me to some documentation / tutorial that explains how to use the syntax above ?

Upvotes: 2

Views: 70

Answers (2)

0xPrateek
0xPrateek

Reputation: 1188

It's actually not list comprehension instead there are two concepts used,

  1. [Dictionary comprehension]
  2. [pandas.Series.str.contains]

Whereas the following code,

{col: data[col].str.contains('#') for col in cols}

Iterates over the list df.columns with the variable col and add it to the resulting list. This syntax is Dictionary comprehension.

Example of dictionary comprehension:

myDict = {x: x**2 for x in [1,2,3,4,5]} 
print (myDict) 

Output

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Upvotes: 1

isstiaung
isstiaung

Reputation: 611

It's a dict comprehension. Dictionaries allow you to map one value onto another which is what you're doing in your code. The official python documentation covers this with a neat example

{x: x**2 for x in (2, 4, 6)}

which returns a dict : {2: 4, 4: 16, 6: 36} where x is mapped to x**2

essentially what you're doing here is,

{key: value that you want for the key, value in dict.items()}

Upvotes: 2

Related Questions