Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[solved] iteration + df.loc
#1
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
Reply
#2
(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
ju21878436312 likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Thank you, I'll use your method!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020