How to select range of columns in a dataframe based on their name and not their indexes?

前端 未结 5 1579
梦谈多话
梦谈多话 2020-12-21 09:50

In a pandas dataframe created like this:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(10, size=(6, 6)),
                  colu         


        
5条回答
  •  一向
    一向 (楼主)
    2020-12-21 10:32

    Adding onto @evan058's answer:

    subset(df[rownames(df) %in% c("r3", "r4", "r5"),], select=c1:c4)
    
    c1 c2 c3 c4
    r3  3  4  5  6
    r4  4  5  6  7
    r5  5  6  7  8
    

    But note, the : operator will probably not work here; you will have to write out the name of each row you want to include explicitly. It might be easier to group by a particular value of one of your other columns or to create an index column as @evan058 mentioned in comments.

提交回复
热议问题