1

I have a list hikari_col which has a two values: column_name1, column_name2. I also have a dataframe df1 which contains this values as column names. I'm trying to check a values from df1 dataframe based on that hikari_col list. So I'm passing a values from list in for loop as a column in dataframe and checking if it throws an error or not.

hikari_cols = ["column_name1", "column_name2"]
df1 = create_dfs("hikari", hikari_cols) # creating a df in another function
    for hikari_col in hikari_cols: # looping over values in list (same as column names in df1)
        try:
            df1.hikari_col # Checking if column was created - It is, and its available as `df1.column_name1`
        except:            # But not as df1.hikari_col
            return err_dict = {"error": "not found"}

But it goes to exception every time. What I'm doing wrong?

1
  • Is create_dfs() a function that you created? What does it do? Commented Jan 20, 2020 at 5:00

1 Answer 1

2

It's because . doesn't work with strings, you have to change df.hikari_col to df[hikari_col], but the most efficient way would be:

hikari_cols = ["column_name1", "column_name2"]
df1 = create_dfs("hikari", hikari_cols) # creating a df in another function
if any(i not in df1.columns for i in hikari_cols):
    err_dict = {"error": "not found"}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! this worked! Just delete one in in your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.