Set a Data Frame Column as the Index of R data.frame object

后端 未结 3 2049
陌清茗
陌清茗 2020-12-29 20:17

Using R, how do I make a column of a dataframe the dataframe\'s index? Lets assume I read in my data from a .csv file. One of the columns is called \'Date\' and I want to ma

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 21:02

    The index can be set while reading the data, in both pandas and R.

    In pandas:

    import pandas as pd
    df = pd.read_csv('/mydata.csv', index_col="Date")
    

    In R:

    df <- read.csv("/mydata.csv", header=TRUE, row.names="Date")
    

提交回复
热议问题