I have a data frame df and would like to reassign value from columns b to the last columns. The logic is as follows: if "b" column value is greater or equal to the previous row of "a" column value, reassign "b" value as "green", otherwise "red". My code raise an indexing error: Too many indexers. Have no idea what's wrong with my code. Any help would be appreciated.
value = [[10, 95, 10, 32],[22, 12, 3, 15],[28, 25, 5, 29],[30, 11, 66, 16]]
df = pd.DataFrame(value, columns=['a', 'b', 'c', 'd'])
for j in range(2, len(df.columns)):
df.iloc[:,j] = df.apply(lambda x: "green" if x.iloc[:,j] >= (x["b"].shift(periods = 1)) else "red", axis = 1)
The expected result is:
a b c d
10 nan nan nan
22 green red green
28 green red green
30 red green red
a? if so wouldn't the last row of C be green as 66 >= 28? Can you elaborate on why the expected values are expected?xis a Series and only has a single dimension. So it would be something likex.iloc[j]but then you'll have other errors raised.xis referring to a row (axis=1) of the DataFrame. Which is a single dimension (Series), not two-dimensional data like a DataFrame.