【问题标题】:Selecting multiple columns from a data frame using dictionary使用字典从数据框中选择多列
【发布时间】:2020-06-07 10:22:17
【问题描述】:

我有一个带有列名的数据框

** RI Na Mg Al Si K Ca Ba Fe 型 **

类型列是分类的,由 4 种类型组成。如何使用字典只提取类型为2的RI、Al、Si、Ba列?

我已尝试使用以下代码:

pd.DataFrame.from_dict(data=df[df['Type']==2], orient='index',columns=['RI','Al','Si','Ba']) 

我收到一个错误:

'numpy.ndarray' object is not callable

如何只选择类型 2 的特定列?

【问题讨论】:

    标签: python pandas numpy dataframe dictionary


    【解决方案1】:

    首先通过filtered_df=df[df[Type ==2]]过滤类型等于2的数据帧的行...现在使用to_dict()将过滤后的df转换为dict
    dict = filters_df.to_dict() 现在从 dict 你可以通过将所需列作为键传递来过滤多个列 键 = ['Ri', 'Ai', 'Si','Ba'] 对于键中的键: dict.get(key)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多