Home »
Python »
Python Programs
How to fix 'Passing list-likes to .loc or [] with any missing labels is no longer supported'?
Learn, how to fix 'Passing list-likes to .loc or [] with any missing labels is no longer supported'?
Submitted by Pranit Sharma, on February 18, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose that we are given a dataframe and we need to modify this dataframe by removing some columns and retaining some specific columns.
Dealing with the error "Passing list-likes to .loc or [] with any missing labels is no longer supported"
For this purpose, we can use the pandas.DataFrame.loc property and pass the sequence of columns that we want to keep but this will throw an error that we have discussed above.
It is because this technique in the modern version of pandas is deprecated and it will show us a warning message on this point. Hence, we need to use the reindexing technique. We will use reindex() method on our dataframe and pass the specific columns which we want to retain inside it.
Let us understand with the help of an example,
Python program to fix 'Passing list-likes to .loc or [] with any missing labels is no longer supported'
# Importing pandas package
import pandas as pd
# Creating a dataframe
df = pd.DataFrame({'A':[1,5,10],'B':[2,7,12],'C':[3,8,13],'D':[4,9,14]})
# Display the DataFrame
print("Original DataFrame:\n",df,"\n\n")
# Columns to keep
col = ['A','C']
# Retaining some columns and removing others
res = df.reindex(columns=col)
# Display result
print("Result:\n",res)
Output
The output of the above program is:
Python Pandas Programs »