Answer to Question #316798 in Python for Vice

Question #316798

If df is a DataFrame that includes a column 'x', what is NOT a way to add a new column 'y', using a function 'f' that applies a transformation?

Select an answer:

  • df.y = f(df.x)
  • df['y'] = None; df.y = f(df.x)
  • df['y'] = f(df['x'])
  • df = df.assign(y = f(df.x))
1
Expert's answer
2022-03-24T01:40:12-0400
df.y = f(df.x)

Pandas doesn't allow columns to be created via a new attribute name. So, pandas sees `y` as an attribute of the dataframe `df` rather than a new column name.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS