dataframe, set index from list

后端 未结 3 904
后悔当初
后悔当初 2021-02-13 17:30

Is it possible when creating a dataframe from a list, to set the index as one of the values?

import pandas as pd

tmp = [[\'a\', \'a1\'], [\'b\',\' b1\']]

df =          


        
相关标签:
3条回答
  • 2021-02-13 17:50

    If you don't want index name:

    df = pd.DataFrame(tmp, columns=["First", "Second"], index=[i[0] for i in tmp])
    

    Result:

      First Second
    a     a     a1
    b     b     b1
    
    0 讨论(0)
  • 2021-02-13 17:58
    >>> pd.DataFrame(tmp, columns=["First", "Second"]).set_index('First', drop=False)
          First Second
    First             
    a         a     a1
    b         b     b1
    
    0 讨论(0)
  • 2021-02-13 18:03

    Change it to list before assigning it to index

    df.index = list(df["First"])
    
    0 讨论(0)
提交回复
热议问题