votes up 3

passes columns are not ALL present dataframe

Package:
pandas
github stars 30911
Exception Class:
KeyError

Raise code

        else:
            self.styler = None
        self.df = df
        if cols is not None:

            # all missing, raise
            if not len(Index(cols).intersection(df.columns)):
                raise KeyError("passes columns are not ALL present dataframe")

            if len(Index(cols).intersection(df.columns)) != len(set(cols)):
                # Deprecated in GH#17295, enforced in 1.0.0
                raise KeyError("Not all names specified in 'columns' are found")

            self.df = df.reindex(columns=cols)
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 1 votes down

When writing a pandas data frame to excel file make sure the passed columns exist in the dataframe.

Steps to reproduce the error:

  • Setup and installation

$ pip install --user pipenv
$ mkdir test_folder
$ cd test_folder
$ pipenv shell
$ pipenv install pandas

  • Run test code

import pandas as pd
import os
import tempfile

df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],index=['row 1', 'row 2'],columns=['col 1', 'col 2'])

with tempfile.TemporaryDirectory() as tmpdir:
    model_path = os.path.join(tmpdir,"output.xlsx")
    df1.to_excel(model_path,columns=["col1","col2"])
# Here col1 and col2 doens't exist in df1. df1's columns are "col 1" and "col 2"

Fixed version of the code

import pandas as pd
import os
import tempfile

df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],index=['row 1', 'row 2'],columns=['col 1', 'col 2'])

with tempfile.TemporaryDirectory() as tmpdir:
    model_path = os.path.join(tmpdir,"output.xlsx")
    df1.to_excel(model_path,columns=["col 1","col 2"])

Jun 01, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix