I have a data frame that looks like this:
> mydf
val1 val2
hsa-let-7a 2.139890 -0.03477569
hsa-let-7b 2.102590 0.04108795
hsa-let-7c 2.061705 0.02375882
hsa-let-7d 1.938950 -0.04364545
hsa-let-7e 1.889000 -0.10575235
hsa-let-7f 2.264296 0.08465690
Note that from 3 columns only 2nd and 3rd are names. What I want to do is to name the first column (plus rename the 2nd and 3rd).
But why this command failed?
colnames(mydf) <- c("COL1","VAL1","VAL2");
What's the right way to do it?
It gave me:
Error in `colnames<-`(`*tmp*`, value = c("COL1", "VAL1", "VAL2" :
'names' attribute [3] must be the same length as the vector [2]
You can use the rename() method of pandas. DataFrame to change column/index name individually. Specify the original name and the new name in dict like {original name: new name} to columns / index parameter of rename() . columns is for the column name, and index is for the index name.
There are situations when an Unnamed: 0 column in pandas comes when you are reading CSV file . The simplest solution would be to read the "Unnamed: 0" column as the index. So, what you have to do is to specify an index_col=[0] argument to read_csv() function, then it reads in the first column as the index.
The solution can be improved as data. rename( columns={0 :'new column name'}, inplace=True ) . There is no need to use 'Unnamed: 0' , simply use the column number, which is 0 in this case and then supply the 'new column name' .
You could join the row names to the dataframe, like this:
mydf <- cbind(rownames(mydf), mydf)
rownames(mydf) <- NULL
colnames(mydf) <- c("COL1","VAL1","VAL2")
Or, in one step:
setNames(cbind(rownames(mydf), mydf, row.names = NULL),
c("COL1", "VAL1", "VAL2"))
# COL1 VAL1 VAL2
# 1 hsa-let-7a 2.139890 -0.03477569
# 2 hsa-let-7b 2.102590 0.04108795
# 3 hsa-let-7c 2.061705 0.02375882
# 4 hsa-let-7d 1.938950 -0.04364545
# 5 hsa-let-7e 1.889000 -0.10575235
# 6 hsa-let-7f 2.264296 0.08465690
this may also work in your case,
mydf <- cbind(rownames(mydf),mydf)
rownames(mydf) <- NULL
colnames(mydf) <- c(names(mydf)) #to not write all the column names
colnames(mydf)[1] <- "name.of.the.first.column"
names(mydf)
If one wants to use a tidyverse
solution within his/her pipeline, this works
rownames_to_column(mydf, var = "var_name")
The function is contained in the tibble
package
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With