Pandas: Change Column Names

Data Wrangling

Difficulty: None | Problem written by bilaldadanlar

Educational Resource: https://web.stanford.edu/class/cs102/readings/pandasreferences.htm

Columns of pandas dataframes can be obtained by either df["column_name"] or df.column_name format. However, if the column name includes a white space between words, you can not use the second one. So its generally a good idea to replace white spaces with underscore.

Write a function that, given a pandas dataframe, replaces all white spaces (" ") in a column name with underscore ("_") and returns the dataframe.

Sample Input:
<class 'str'>
df: first name last name 0 Abraham Lincoln 1 Richard Nixon

Expected Output:
<class 'pandas.core.frame.DataFrame'>
0 0 Abraham,Lincoln 1 Richard,Nixon 2

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

This is a premium feature.
To access this and other such features, click on upgrade below.

Log in to post a comment


Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)