How to select the last column of dataframe

后端 未结 7 741
北恋
北恋 2020-11-30 01:18

I have done some searching for the answer to this question, but all I can figure out is this:

df[df.columns[len(df.columns)-1]]

which to m

相关标签:
7条回答
  • 2020-11-30 01:58

    Use iloc and select all rows (:) against the last column (-1):

    df.iloc[:,-1:]
    
    0 讨论(0)
  • 2020-11-30 02:05
    df.T.iloc[-1]
    

    df.T.tail(1)
    

    pd.Series(df.values[:, -1], name=df.columns[-1])
    
    0 讨论(0)
  • 2020-11-30 02:11

    These are few things which will help you in understanding everything... using iloc

    In iloc, [initial row:ending row, initial column:ending column]

    case 1: if you want only last column --- df.iloc[:,-1] & df.iloc[:,-1:] this means that you want only the last column...

    case 2: if you want all columns and all rows except the last column --- df.iloc[:,:-1] this means that you want all columns and all rows except the last column...

    case 3: if you want only last row --- df.iloc[-1:,:] & df.iloc[-1,:] this means that you want only the last row...

    case 4: if you want all columns and all rows except the last row --- df.iloc[:-1,:] this means that you want all columns and all rows except the last column...

    case 5: if you want all columns and all rows except the last row and last column --- df.iloc[:-1,:-1] this means that you want all columns and all rows except the last column and last row...

    0 讨论(0)
  • 2020-11-30 02:13

    Somewhat similar to your original attempt, but more Pythonic, is to use Python's standard negative-indexing convention to count backwards from the end:

    df[df.columns[-1]]
    
    0 讨论(0)
  • 2020-11-30 02:17

    This is another way to do it. I think maybe a little more general:

    df.ix[:,-1]
    
    0 讨论(0)
  • 2020-11-30 02:19

    The question is: how to select the last column of a dataframe ? Appart @piRSquared, none answer the question.

    the simplest way to get a dataframe with the last column is:

    df.iloc[ :, -1:]
    
    0 讨论(0)
提交回复
热议问题