1

I have the following DataFrame:

In [282]: f
Out[282]: 
            Strike
01-02-2019   100.0
01-02-2019   105.0
01-02-2019   110.0
01-02-2019   115.0
01-02-2019   120.0

and I want to parse the entry from each position to a method and populate a new column like this:

def method(i,j):
    return(i,j)

for i,j in f.index, f.Strike: 
    f['newcol']=method(i,j) 

but am receiving an error. How would I do this?

bigboat
  • 249
  • 1
  • 4
  • 13

1 Answers1

1

Use Series.items in list comprehension:

df['new'] = [method(k, v) for k, v in df.Strike.items()]

Another solution with zip:

df['new'] = [method(k, v) for k, v in zip(df.index, df.Strike)]

Or use DataFrame.apply:

df['new'] = df.apply(lambda x: method(x.name, x.Strike), axis=1)

print (df)
            Strike                  new
01-02-2019   100.0  (01-02-2019, 100.0)
01-02-2019   105.0  (01-02-2019, 105.0)
01-02-2019   110.0  (01-02-2019, 110.0)
01-02-2019   115.0  (01-02-2019, 115.0)
01-02-2019   120.0  (01-02-2019, 120.0)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252