【问题标题】:A function to recall column from a csv file从 csv 文件中调用列的函数
【发布时间】:2019-07-27 07:18:58
【问题描述】:

我需要创建一个函数,以便我可以按列提取数据。例如,df 具有列状态我想查找特定状态的所有相关机场信息,比如 IL

def state(df, state='IL')

csv 已加载到数据框中。

我试过了:

def find_state(df, state='IL')
    df_state = df[df.state =='IL']
    return df_state

find_state(df, state='IL')

【问题讨论】:

  • 您已将df_state = df[df.state =='IL'] 硬编码到您的函数中。更改它以将传入的状态变量作为函数参数,你应该没问题。

标签: python dataframe filtering


【解决方案1】:

您已经将“IL”作为state kwarg 的默认值,并将“IL”硬编码到函数中。将“IL”替换为参数state,它应该可以工作。像这样:

def find_state(df, state='IL')
    df_state = df[df.state == state]
    return df_state

【讨论】:

  • 上帝保佑你的灵魂
猜你喜欢
  • 2018-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-09-12
  • 1970-01-01
  • 2020-02-18
  • 2020-02-13
  • 2021-04-28
  • 2015-02-14
相关资源
最近更新 更多