Remove or drop rows from pandas DataFrame

The expression in brackets of pandas DataFrame means the condition.

import pandas

a = [
    ('Texas', 29),
    ('New York', 19),
    ('California', 40),
    ('Florida', 21),
]

df = pandas.DataFrame(a, columns=['State', 'Population'])
df2 = df[df['Population'] < 25]

print(df)
#         State  Population
# 0       Texas          29
# 1    New York          19
# 2  California          40
# 3     Florida          21

print(df2)
#       State  Population
# 1  New York          19
# 3   Florida          21

df['Population'] < 25 in brackets is the condition that the new DataFrame meets.

Comments

Powered by Markdown

More