Renaming Columns | #6 of 53: The Complete Pandas Course

preview_player
Показать описание

-------------------

Now let's try to rename some of the columns present in our churn dataset. Import NumPy and pandas import the data set. And we have the columns, you can access all the columns of your data set using this particular attribute called columns.

This will give all the columns as an index object. This contains all the column names. Now, what we want to do is we want to rename certain column names into a different format. There are different ways to rename it, you can rename the columns in bulk. That is, we want to change all of these names to capital case, you can do that. Or if you want to replace all this space inside this space inside these names with an underscore you can do that as well in one single shot.

So we will look at the various techniques now. So you can do the renaming using this particular function called df dot rename inside that pass in the columns attribute. If you want to rename a particular column, just pass in columns, existing column name followed by the new column name that particular column wants to hold, just give this on running this, that particular column alone will get renamed as you wish. So account length now has an underscore in between, right, this is very much the most used use case of renaming.

Now, another method is you want to rename a lot of columns in one shot, let's say you want to convert all the strings, all the column names to uppercase, you can do that using str dot upper. But before we go to this, I missed one statement here, you can do this renaming. In this particular data frame, we renamed this data frame. But if you look at the column names of df, it is still unaffected. Right? All the column names, if you look at the account length over here, it is still the same, the underscore has not happened. So you can make that change reflect by running this command by adding one additional parameter which is in place equal to true, what this will do is on running this command, it will change the column names of df in place. Now on running this, you will see that df dot columns now has account underscore length, the column names of df has permanently changed now.

Alright, so this is something that is very useful as well. Now let's come back to this one, how to rename all the columns to uppercase or lowercase or title case, whatever it is all the functions of the string object in Python, all of this is available inside str dot, then method name, the corresponding method name. Now you pass this method set access to columns. on doing this, you will see that all the column names have been renamed to contain capital case. Likewise, you can do it for lowercase as well, this will change it to lowercase it was already lowercase. So no change here. I'll change this back to upper.

Alright, so that is one way. Now the third way is you can rename all of these columns in one shot, say you want to replace the space with the underscore character define a lambda function can be a regular function as well, you can define a function here, passing this parameter, the parameter that this function will take will be the column names of all the columns present in your data frame, it will take the column names, you need to set x is equal to column.
Рекомендации по теме
Комментарии
Автор

Want to learn end-to-end Data Science (Machine Learning & AI)?

machinelearningplus