In Pandas, which method is used to apply a custom function to each element of a DataFrame or Series?

pivot()
melt()
apply()
sort_values()

The correct answer is C. apply().

The apply() method is used to apply a custom function to each element of a DataFrame or Series. The function can be any Python function that takes a single argument, which is the value of the element. The function can return any value, which will be assigned to the element.

The apply() method can be used to perform a variety of operations on a DataFrame or Series, such as:

  • Converting the data to a different format
  • Calculating new values
  • Performing statistical operations

The apply() method is a powerful tool that can be used to perform a variety of operations on a DataFrame or Series.

The other options are incorrect because:

  • The pivot() method is used to pivot a DataFrame, which is a process of converting a DataFrame from a rectangular shape to a square shape.
  • The melt() method is used to melt a DataFrame, which is a process of converting a DataFrame from a wide shape to a long shape.
  • The sort_values() method is used to sort a DataFrame by one or more columns.