I have a data frame with 36 columns and more than 3000 rows. I am using plot function inside a for loop
to plot graphs of each column. I want the title of the graph to appear as column name. How can I do that?
for(i in c(1:36)){
plot(DowData[,i],type="l",main="colnames(DowData)[i]")
}
You can get the column names from pandas DataFrame using df. columns. values , and pass this to python list() function to get it as list, once you have the data you can print it using print() statement.
Method 1: using colnames() method colnames() method in R is used to rename and replace the column names of the data frame in R. The columns of the data frame can be renamed by specifying the new column names as a vector. The new name replaces the corresponding old name of the column in the data frame.
Adding column name to the DataFrame : We can add columns to an existing DataFrame using its columns attribute. Output : Now the DataFrame has column names. Renaming column name of a DataFrame : We can rename the columns of a DataFrame by using the rename() function.
Using lapply
you can loop over columns names:
invisible(lapply(colnames(DowData),function(x){
plot(DowData[,x],main=x,type="l")
}))
data <- read.csv("sample.csv",header=T,sep=",")
for ( i in seq(1,length( data ),1) ) plot(data[,i],ylab=names(data[i]),type="l")
This should Work
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