I want to create a modified dataframe with the specified columns.
I tried the following but throws the error “Passing list-likes to .loc or [] with any missing labels is no longer supported”

# columns to keep
filtered_columns = ['text', 'agreeCount', 'disagreeCount', 'id', 'user.firstName', 'user.lastName', 'user.gender', 'user.id']
tips_filtered = tips_df.loc[:, filtered_columns]

# display tips

Thank you

It looks like Pandas has deprecated this method of indexing. According to their docs:

This behavior is deprecated and will show a warning message pointing
to this section. The recommended alternative is to use .reindex()

Using the new recommended method, you can filter your columns using:

tips_filtered = tips_df.reindex(columns = filtered_columns).

NB: To reindex rows, you would use reindex(index = ...) (More information here).

Some of the columns in the list are not included in the dataframe , if you do want do that , let us try reindex

tips_filtered = tips_df.reindex(columns=filtered_columns)

I encountered the same error with missing row index labels rather than columns.
For example, I would have a dataset of products with the following ids: [‘a’,’b’,’c’,’d’]. I store those products in a dataframe with indices ['a','b','c','d']:

df=pd.DataFrame(['product a','product b','product c', 'product d'],index=['a','b','c','d'])

Now let’s assume I have an updated product index:
row_indices=['b','c','d','e'] in which ‘e’ corresponds to a new product: ‘product e’. Note that ‘e’ was not present in my original index ['a','b','c','d'].

If I try to pass this updated index to my df dataframe: df.loc[row_indices,:],

I’ll get this nasty error message:

KeyError: “Passing list-likes to .loc or [] with any missing labels is no longer supported. The following labels were missing: Index([‘e’], dtype=”object”).

To avoid this error I need to do intersection of my updated index with the original index:


this is in line with recommendation of what pandas docs

This error pops up if indexing on something which is not present – reset_index() worked for me as I was indexing on a subset of the actual dataframe with actual indices, in this case the column may not be present in the dataframe.

I had the same issue while trying to create new columns along with existing ones?:

df = pd.DataFrame([[1,2,3]], columns=["a","b","c"])
def foobar(a,b):
  return a,b
df[["c","d"]] = df.apply(lambda row: foobar(row["a"], row["b"]), axis=1)

The solution was to add result_type="expand" as an argument of apply()?:

df[["c","d"]] = df.apply(lambda row: foobar(row["a"], row["b"]), axis=1, result_type="expand")