Changing column names of a data frame

0 votes
I have a data frame called "newprice" (as seen below), and I'd like to update the column names in my R application.

> newprice Chang. Chang. Chang. 1 100 36 136 2 120 -33 87 3 150 14 164 Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang. Chang.
In fact, I'm doing exactly that:

names(newprice)[1]

<-paste("premium")\snames(newprice)[2]

<-paste("change")\snames(newprice)[3]

<-paste("newprice")
I didn't use a loop since I wanted each column name to be unique, as you can see.

This is the output I get when I paste my programme into the R console:

> newprice(names)[1]

Error: unknown input in -paste("premium") "[1] names(newprice)

-paste(" -paste(" -paste(" -p "> names(newprice)(newprice)(newprice)(newprice)(newprice)

<-paste(“change”)
Unexpected input resulted in an error "names(newprice)(newprice)(newprice)(newprice)(newprice
-paste(" -paste(" -paste(" -p "> newprice(names)[3]
<-paste(“newpremium”)
Unexpected input resulted in an error "newprice(names)[3]
<-paste(“"
I've also tried replacing the paste() function with the c() function—for example, c("premium")—but to no avail.

Could someone please assist me in figuring this out?
Jun 14, 2022 in Data Science by Avinash
• 1,260 points
253 views

1 answer to this question.

0 votes
import pandas as pd

#define DataFrame
df = pd.DataFrame({'team':['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#list column names
list(df)

['team', 'points', 'assists', 'rebounds']

#rename specific column names
df.rename(columns = {'team':'team_name', 'points':'points_scored'}, inplace = True)

#view updated list of column names
list(df)

['team_name', 'points_scored', 'assists', 'rebounds']

Discover the world of data with our Data Science Course and embark on a transformative journey towards analytical excellence.

answered Jun 24, 2022 by Sohail
• 3,040 points

Related Questions In Data Science

0 votes
0 answers

Determine the data types of a data frame's columns

I'm using R and have loaded data ...READ MORE

Jun 1, 2022 in Data Science by avinash
• 1,840 points
176 views
0 votes
0 answers

To speed up the tapply function in R, or another function to convert data frame into a matrix

I must turn a sizable dataset into ...READ MORE

Jun 24, 2022 in Data Science by Sohail
• 3,040 points
192 views
0 votes
0 answers

How do I get a list of built-in data sets in R?

Please advise on how to obtain the ...READ MORE

Jul 5, 2022 in Data Science by avinash
• 1,840 points
176 views
0 votes
1 answer

grepl in R to find matches to any of a list of character strings

Inside of a grepl regular expression, you ...READ MORE

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
2,383 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,241 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,360 points
753 views
0 votes
1 answer
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,605 views
0 votes
1 answer

Using dcast to widen a data frame

> dcast(z, id ~ item, value.var="freq") ...READ MORE

answered Jun 24, 2022 in Data Science by Sohail
• 3,040 points
256 views
0 votes
1 answer

Filter multiple values on a string column in dplyr

The filter() function is used to select ...READ MORE

answered Jun 14, 2022 in Data Science by Sohail
• 3,040 points
801 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP