Feb-15-2022, 08:14 AM
(This post was last modified: Feb-15-2022, 01:59 PM by ju21878436312.)
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?
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 dfwhat 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 9what 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