1

In a specific data set, I have a column, 'starCustomer', that takes on these values:

[0, X, 0, 0, X, 0, X,...]

That is, each observation will contain a 0 if the person is not a Star Customer, but an X if that person is. I thought it would be a better idea to represent each X as 1 instead, so, I wrote the following code:

Star = df['starCustomer']
New_Star = [1 if x == 'X', else 0 for x in Star]

However, it is to my knowledge that New_Star is not a data frame, as we want it to be. So, I try to execute this following code:

Star = pd.DataFrame(New_Star)

However, I get the following error:

TypeError: 'list' object is not callable

Can anybody inform me on what's incorrect about this?

1
  • In your example Star is not a DataFrame either. It is a Series. Commented Mar 20, 2016 at 22:03

2 Answers 2

1

A column in a dataframe is an object of the class pd.Series

You can first add a new column to the dataframe:

df['New_Star'] = df.apply(lambda x: 1 if x == 'X' else 0 , axis=1)

You can now get a dataframe consisting only on the column New_Star with:

new_star_df = df[['New_Star']]

Note the double brackets, with a single bracket you will return the pd.Series, not the pd.DataFrame

Sign up to request clarification or add additional context in comments.

Comments

0

If I understand you right, you can just do this:

New_Star = Star.map({0: 0, "X": 1})

2 Comments

I tried that, but it didn't take care of the Data Frame problem! Also, when I type in New_Star just to see the data itself, I get an error about isinstance().
Actually, with some rearranging of the syntax, this approach works like a charm. Thank you!

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.