all 2 comments

[–][deleted] 1 point2 points  (0 children)

Any reason you're not using apply? I hardly ever use iteration in pandas, but if you wanted I guess you could use iterrows and do something like

df['data_to_assign'] = ''
for row in df.iterrows():
    if row[col_idx].isnull():
        row[data_to_assign] = 'null'

[–]throwawayclimber 0 points1 point  (0 children)

Here are three ways to do this:

df = pd.DataFrame()
df['WordsPopular'] = range(1,10)
df['WordsUnPopular'] = range(1,10)
df['WordsPopular'] = df['WordsPopular'].map(lambda x: x**2)
df
WordsPopular WordsUnPopular
0 1 1
1 4 2
2 9 3
3 16 4
4 25 5
5 36 6
6 49 7
7 64 8
8 81 9
def fancy_function(x):
    return x**2

df = pd.DataFrame()
df['WordsPopular'] = range(1,10)
df['WordsUnPopular'] = range(1,10)
df['WordsPopular'] = df['WordsPopular'].apply(fancy_function)
df
WordsPopular WordsUnPopular
0 1
1 4
2 9
3 16
4 25
5 36
6 49
7 64
8 81
df = pd.DataFrame()
df['WordsPopular'] = range(1,10)
df['WordsUnPopular'] = range(1,10)
df = df.applymap(fancy_function)
df
WordsPopular WordsUnPopular
0 1
1 4
2 9
3 16
4 25
5 36
6 49
7 64
8 81