pandas: best way to select all columns whose names start with X

前端 未结 8 1160
别那么骄傲
别那么骄傲 2020-11-27 10:03

I have a DataFrame:

import pandas as pd
import numpy as np

df = pd.DataFrame({\'foo.aa\': [1, 2.1, np.nan, 4.7, 5.6, 6.8],
                   \'foo.fighters         


        
相关标签:
8条回答
  • 2020-11-27 10:39

    In my case I needed a list of prefixes

    colsToScale=["production", "test", "development"]
    dc[dc.columns[dc.columns.str.startswith(tuple(colsToScale))]]
    
    0 讨论(0)
  • 2020-11-27 10:42

    The simplest way is to use str directly on column names, there is no need for pd.Series

    df.loc[:,df.columns.str.startswith("foo")]
    
    
    
    0 讨论(0)
提交回复
热议问题