Thomas Philips
Thomas Philips

Reputation: 1089

How to operate on a dataframe column by index

Following advice I received on StackOverflow, I thought I had solved my `CopyWithWarning problems when using Pandas dataframes, but they are back to bite me. I'm trying to create a new column in a dataframe that is the first difference of another column:

df[upper_sys_hrt] = df[idx_upper] - df[idx_upper].shift(1)
df[upper_sys_hrt].iloc[0] = d[upper_sys_hrt].iloc[1]

This sometimes (not always) gives me a CopyWithWarning error. I tried changing this to

df[ : , df.get_loc(upper_sys_hrt)] =  df[ : , df.columns.get_loc(idx_upper)] - df[: , df.columns.get_loc(idx_upper)].shift(1)

but all i get is an error message:

TypeError: '(slice(1, None, None), 3)' is an invalid key

I have tried (hunt and peck) changes to the syntax as well as Googling and gotten nowhere. What am I doing wrong?

Upvotes: 0

Views: 72

Answers (1)

cs95
cs95

Reputation: 402814

You have the column's position, not the label. To get the label, index back into df.columns:

col = df.columns[df.get_loc(upper_sys_hrt)]
df[col] = df[col] - df[col].shift()

Or just

df[col] = df[col].diff()

If you're rather work with the index, you can use iloc:

idx = df.get_loc(upper_sys_hrt)
df.iloc[:,idx] = df.iloc[:,idx].diff()

Upvotes: 1

Related Questions