Python Forum
[solved] iteration + df.loc - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [solved] iteration + df.loc (/thread-36388.html)



[solved] iteration + df.loc - ju21878436312 - Feb-15-2022

I'd like to iterate through the lines by a for loop.
And when the condition "df['col2'] == "second"is true, I'd like to do some calculations in this line in column df["col3"], e.g. df.loc[i, "col3"] = i.
Could someone give me a hint?


import pandas as pd
import numpy as np

d = {'col1': [95, 82, 71, 93, 91, 16, 78, 81, 90, 89], 'col2': ["first", "first", "first", "first", "second", "second", "second", "second", "second", "second"]}

df = pd.DataFrame(data=d)
df


df["col3"]=""

for i in range(len(df["col1"])):
    df.loc[(df['col2']  == "second"),"col3"]=i
    
df
what I get is:

col1 	col2 	col3
95 	first 	
82 	first 	
71 	first 	
93 	first 	
91 	second 	9
16 	second 	9
78 	second 	9
81 	second 	9
90 	second 	9
89 	second 	9
what I want to get is:

col1 	col2 	col3
95 	first 	
82 	first 	
71 	first 	
93 	first 	
91 	second 	5
16 	second 	6
78 	second 	7
81 	second 	8
90 	second 	9
89 	second 	10



RE: iteration + df.loc - perfringo - Feb-15-2022

(Feb-15-2022, 08:14 AM)ju21878436312 Wrote: I'd like to iterate through the lines by a for loop.

Why? This is pandas and usually it's not good idea to iterate over rows or columns of dataframe. Instead use pandas and numpy built-in methods.

Its unclear what values must be in col3, based on code provided I assume that row index:

import pandas as pd
import numpy as np

data = {'col1': [95, 82, 71, 93, 91, 16, 78, 81, 90, 89],
        'col2': ["first", "first", "first", "first", "second", "second", "second", "second", "second", "second"]}

df = pd.DataFrame(data)

df['col3'] = np.where(df['col2']=='second', df.index, "")
Output:
col1 col2 col3 0 95 first 1 82 first 2 71 first 3 93 first 4 91 second 4 5 16 second 5 6 78 second 6 7 81 second 7 8 90 second 8 9 89 second 9



RE: [solved] iteration + df.loc - ju21878436312 - Feb-15-2022

Thank you, I'll use your method!