I am trying to drop the columns of a Pandas Dataframe based on the value of the columns of a second Boolean array (that has the same length).
For example suppose the pandas dataframe
[value1, value2, value3]
and a boolean array
[True, False, True].
The result of the dropping operation on the Pandas dataframe would be
[value1, value3]
Notice that we dropped the second column that has value False on the boolean array.
I guess something like this would work fine:
for i in range(len(boolean_array)):
if boolean_array[i] == False:
df = df.drop(df.columns[i], axis=1)
but I am pretty sure there is an easier way.
Use boolean indexing
with DataFrame.loc
for filter columns by boolean mask:
df = pd.DataFrame({'value1':[1,2],
'value2':[10,20],
'value3':[100,200]})
arr = np.array([True, False, True])
df1 = df.loc[:, arr]
print (df1)
value1 value3
0 1 100
1 2 200
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.