Delete Rows of pandas DataFrame Conditionally in Python (Example) | Remove & Drop Multiple & One Row

preview_player
Показать описание
Python code of this video:

import pandas as pd # Import pandas library in Python

data = pd.DataFrame({"x1":range(1, 7), # Create pandas DataFrame
"x2":["a", "b", "c", "d", "e", "f"],
"x3":[5, 1, 5, 1, 5, 1]})
print(data) # Print pandas DataFrame

data1 = data[data.x3 != 5] # Using logical condition
print(data1) # Print updated DataFrame

print(data2) # Print updated DataFrame

data3 = data[(data["x3"] != 5) & (data["x1"] > 2)] # Multiple logical conditions
print(data3) # Print updated DataFrame

my_list = ["yes", "yes", "no", "yes", "no", "yes"] # Create example list
print(my_list) # Print example list
# ['yes', 'yes', 'no', 'yes', 'no', 'yes']

data4 = data[[x == "yes" for x in my_list]] # Using list to remove rows
print(data4) # Print updated DataFrame

Follow me on Social Media:

Рекомендации по теме
Комментарии
Автор

Thanks so much for the video! Amazing and clear everything until the last example - #using the list to remove rows. What do you mean by - "yes" that corresponds to the raws in data frame data4? I am confused about how the string "yes" corresponds to any raw in a data frame that doesn't contain the same string but just numbers?

Aleqsie