How to drop a list of rows from Pandas dataframe?
Use DataFrame.drop and pass it a Series of index labels:
In [65]: df
Out[65]:
one two
one 1 4
two 2 3
three 3 2
four 4 1
In [66]: df.drop(df.index[[1,3]])
Out[66]:
one two
one 1 4
three 3 2
Drop a list of rows from Pandas df
For dropping rows:
df.drop(rows,inplace=True)
For dropping columns:
df.drop(cols,axis=1,inplace=True)
Test:
df = pd.DataFrame({'A':[1,2,3,4,5],'B':[1,2,3,4,5]})
print('DataFrame with initialised :\n',df,'\n')
df.drop([2,4],inplace=True)
print('DataFrame after dropping 2 rows:\n',df,'\n')
df.drop(['B'],axis=1,inplace=True)
print('DataFrame after dropping 1 column:\n',df,'\n')
Output:
DataFrame with initialised :
A B
0 1 1
1 2 2
2 3 3
3 4 4
4 5 5
DataFrame after dropping 2 rows:
A B
0 1 1
1 2 2
3 4 4
DataFrame after dropping 1 column:
A
0 1
1 2
3 4
dropping rows from dataframe based on a not in condition
You can use pandas.Dataframe.isin
.
pandas.Dateframe.isin
will return boolean values depending on whether each element is inside the list a
or not. You then invert this with the ~
to convert True
to False
and vice versa.
import pandas as pd
a = ['2015-01-01' , '2015-02-01']
df = pd.DataFrame(data={'date':['2015-01-01' , '2015-02-01', '2015-03-01' , '2015-04-01', '2015-05-01' , '2015-06-01']})
print(df)
# date
#0 2015-01-01
#1 2015-02-01
#2 2015-03-01
#3 2015-04-01
#4 2015-05-01
#5 2015-06-01
df = df[~df['date'].isin(a)]
print(df)
# date
#2 2015-03-01
#3 2015-04-01
#4 2015-05-01
#5 2015-06-01
How to drop rows from a pandas dataframe based on a pre-made list
You can do this using pandas.Dataframe.isin
. This will return boolean values checking whether each element is inside the list x
. You can then use the boolean values and take out the subset of the df
with rows that return True
by doing df[df['City'].isin(x)]
. Following is my solution:
import pandas as pd
x = ['Paris' , 'Marseille']
df = pd.DataFrame(data={'City':['Paris', 'London', 'New York', 'Marseille'],
'Age':[1, 2, 3, 4]})
print(df)
df = df[df['City'].isin(x)]
print(df)
Output:
>>> City Age
0 Paris 1
1 London 2
2 New York 3
3 Marseille 4
City Age
0 Paris 1
3 Marseille 4
Pandas: How to remove rows from a dataframe based on a list?
You need
new_df = sales[~sales.CustomerID.isin(badcu)]
PANDAS drop a range of rows from df
Use slice to select the part you want:
df[:-m]
If you want to remove some middle rows, you can use drop
:
df.drop(df.index[3:5])
Drop rows by index from dataframe
Change it to
df_train.drop(wrong_indexes_train,axis=1)
Dropping list of rows from multi-level pandas dataframe without a loop
Create the MultiIndex
index then drop it
dfmi.drop(pd.MultiIndex.from_arrays([As,Bs]))
Droping a range of rows in pandas
The best way to do this is by indexing: df.drop(df.index[0:9])
. This example will drop the first ten rows.
Related Topics
Editing the Date Formatting of X-Axis Tick Labels in Matplotlib
Typeerror: '<=' Not Supported Between Instances of 'Str' and 'Int'
Why Is Tkinter Entry's Get Function Returning Nothing
How to Read a File with a Semi Colon Separator in Pandas
Recursion and Return Statements
Removing Item from List Causes the List to Become Nonetype
Compiling Numpy with Openblas Integration
Format Output String, Right Alignment
Python Setup.Py Develop VS Install
What Is the Purpose of Class Methods
Passing Functions with Arguments to Another Function in Python
Pandas: Convert Categories to Numbers
Explaining the 'Self' Variable to a Beginner
How to Get the Path of the Python Script I am Running In
How to Find Length of Digits in an Integer