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

前端 未结 5 1597
梦谈多话
梦谈多话 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:30

    This seems way too easy so perhaps I'm doing something wrong.

    df <- data.frame(c1=1:6, c2=2:7, c3=3:8, c4=4:9, c5=5:10, c6=6:11,
                     row.names=c('r1', 'r2', 'r3', 'r4', 'r5', 'r6'))
    
    
    df[c('r1','r2'),c('c1','c2')]
    
       c1 c2
    r1  1  2
    r2  2  3
    

提交回复
热议问题