Can anyone explain to me what is going on? Here's the piece of code. If I have the DataFrame of precisely length 4, the statement in the try clause throws an exception. If I make the dataframe of any other size than 4, it works. Moreover, if I remove a column 'temp2' and make a DataFrame of length 3, it will also produce exception cannot reshape array of size 3 into shape (3,3). All I want to do is put zeroes for columns where value staleness is less or equal than 1. I'd rather use the list than create a new DataFrame of all zeroes. Thanks in advance.
import pandas as pd
zeroeble_cols = ['volume','trade_count', 'temp', 'temp2']
stalenesses = [0.3, 0.4, 1.2, 3.4]
length = len(stalenesses)
df = pd.DataFrame(data = {'volume' : [10 for i in range(length)],
'trade_count' : [10 for i in range(length)],
'temp' : [1 for i in range(length)],
'temp2' : [1 for i in range(length)],
'staleness' : stalenesses})
try:
df[zeroeble_cols] = df[zeroeble_cols].where(df['staleness'] <= 1, [0 for i in range(len(zeroeble_cols))], axis = 1)
except Exception as e:
print(f'Exception: {e}')
zeroDF = pd.DataFrame(data = {k : [0] for k in zeroeble_cols})
df[zeroeble_cols] = df[zeroeble_cols].where(df['staleness'] <= 1, zeroDF, axis = 1)
print(df)
Everything is described above.
In the
tryblock you have a DataFrame of length 4, and thewheremethod is trying to reshape thelist[0, 0, 0, 0]into a shape of(4, 4)to match the shape of the selected subset of the DataFrame, resulting in an error.Using
where, you should replace[0 for i in range(len(zeroeble_cols))]with just0onaxis=0(the default), and Pandas will set the appropriate rows to this value on the selected columns (zeroeble_cols). Moreover, if you're usingwhere, you should include in the condition the rows that you want to keep as they are, and Pandas will change the other rows according to the condition:You can also use
maskwhich is kind of the opposite ofwhere, so you include in the condition the rows that you want to change:Or you can use
loc, which I tend to prefer:All these options will yield the same result: