A Grammar comprehension problem in python pandas dataFtame

does not quite understand how to filter the first column < 1.5 data in the second sentence with two square brackets.

Dictionary and array are not used in this way either

related codes

df=DataFrame(np.random.randn(20,2),columns=["first","second"])
df["first"][df["first"]<-1.5)]=None

Thank you very much

Jun.24,2021

df['first'][df['first']<-1.5)]=None

first of all, is this code wrong? Is there an extra parenthesis?
the correct one should be:

df['first'][df['first']<-1.5]=None
In

DataFrame, df ['first'] represents the selected field or column named first, and the extracted column is actually Series
df [' first'] <-1.5 which represents a vectorized comparison of the Series. The final result is a list of Boolean values obtained by comparing each value in the Series with 1.5
so, df'first' <-1.5], the Boolean value of Series is indexed
finally, Df'first' <-1.5] = None, is just scalar assignment


tells you an easy way to understand this
plus a print function to output the value of df ['first'] to see what it is?
print (df ['first'])
print (type (df [' first'])

Menu