I have the following line of code
df["high_int"] = df.Slope * (df.index - df.max_idx) df,loc['max_idx', 'High]
max_idx contains the indexes of the highest highs over a period eg: 15 or 30.
I have tried .loc, .iloc, .at, .iat .get, .shift(), as well as df['max_idx'].map(df['High'])
Most errors seem to be related to using a series rather than an int (in the case of .iloc) or similar. It seems to me that this should a trivial task. Am I missing something obvious?
Thanks in advance
CodePudding user response:
Last part doesn't really make sense, df.loc[index, columns]
takes index filters, and column, or list of columns, not 2 columns. Another thing - assuming you wanted to write df[["max_id", "High"]]
- it would also fail, since you cannot force 2 columns into one in this way.
Consider the below as example of what you can and cannot do:
>>> df =pd.DataFrame({"Slope": [1,3,2, -5, -23.3], "max_id": [1,1,1,2,2], "High": [3,4,4,4,3]})
>>> df["high_int"] = df.Slope * (df.index - df.max_id)
>>> df
Slope max_id High high_int
0 1.0 1 3 -1.0
1 3.0 1 4 0.0
2 2.0 1 4 2.0
3 -5.0 2 4 -5.0
4 -23.3 2 3 -46.6
>>> df["high_int"] = df.Slope * (df.index - df.max_id) df[["max_id", "High"]]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/9skibi2/miniconda3/envs/airflow2/lib/python3.9/site-packages/pandas/core/frame.py", line 3967, in __setitem__
self._set_item_frame_value(key, value)
File "/home/9skibi2/miniconda3/envs/airflow2/lib/python3.9/site-packages/pandas/core/frame.py", line 4097, in _set_item_frame_value
raise ValueError("Columns must be same length as key")
ValueError: Columns must be same length as key