Archive
Python – Delete/remove unwanted rows from a DataFrame
As you start using Python you will fall in love with it, as its very easy to solve problems by writing complex logic in very simple, short and quick way. Here we will see how to remove rows from a DataFrame based on an invalid List of items.
Let’s create a sample Pandas DataFrame for our demo purpose:
import pandas as pd
sampleData = {
'CustId': list(range(101, 111)),
'CustomerName': ['Cust'+str(x) for x in range(101, 111)]}
cdf = pd.DataFrame(sampleData)
invalidList = [102, 103, 104]
The above logic in line # 4 & 5 creates 10 records with CustID ranging from 101 to 110 and respective CustomerNames like Cust101, Cust102, etc.
In below code we will use isin() function to get us only the records present in Invalid list. So this will fetch us only 3 invalid records from the DataFrame:
df = cdf[cdf.CustId.isin(invalidList)] df
And to get the records not present in InvalidList we just need to use the “~” sign to do reverse of what we did in above step using the isin() function. So this will fetch us other 7 valid records from the DataFrame:
df = cdf[~cdf.CustId.isin(invalidList)] df
Python error – Length of passed values is 6, index implies 2 (while doing PIVOT with MultiIndex or multiple columns)
As I’m new to Python and these days using it for some Data Analysis & Metadata handling purpose, and also being from SQL background here I’m trying to use as many analysis features I use with SQL, like Group By, Aggregate functions, Filtering, Pivot, etc.
Now I had this particular requirement to PIVOT some columns based on multi-index keys, or multiple columns as shown below: