【问题标题】:Pandas - printing limited rows based on a value in one columnPandas - 根据一列中的值打印有限的行
【发布时间】:2016-12-10 05:22:09
【问题描述】:

我想通过根据特定列中的值选择行来限制正在打印的行。

例如:

Column1, Column2, Column3
aaa, bbb, ccc
none, ddd, ggg

我只想打印Column1值为none的行。

这是我的代码:

for v in df:
    if 'none' in df['2nd_prize']:
        print v

【问题讨论】:

  • 请展示您的尝试。
  • for v in df['2nd_prize']: if 'none' in df['2nd_prize']: print v
  • 我建议您将带有示例输入的完整示例放入您的问题中,而不是在 cmets 中添加行。
  • @merlin2011 我添加了代码。
  • 您的解析似乎存在空格问题。稍后我会发布答案。

标签: python csv pandas indexing dataframe


【解决方案1】:
for row in table:
  if row[0] is none:
    print row

【讨论】:

    【解决方案2】:

    您可以使用loc 对数据框的行进行子集化,以限制Column 1 中包含"none" 的行,如图所示:

    数据准备

    In [1]: import pandas as pd
       ...: from io import StringIO
       ...: 
    
    In [2]: df = pd.read_csv(StringIO(
       ...: '''
       ...: Column1, Column2, Column3
       ...: aaa, bbb, ccc
       ...: none, ddd, ggg
       ...: kkk, jjj, ppp
       ...: none, eee, fff
       ...: '''))
    

    运营

    In [3]: df.loc[df['Column1'] == "none"]
    Out[3]: 
      Column1  Column2  Column3
    1    none      ddd      ggg
    3    none      eee      fff
    

    【讨论】:

      【解决方案3】:

      您可以将boolean indexing 与掩码一起使用:

      import pandas as pd
      
      df = pd.DataFrame({'Column2': {0: 'bbb', 1: 'ddd'}, 
                         'Column1': {0: 'aaa', 1: 'none'}, 
                         'Column3': {0: 'ccc', 1: 'ggg'}})
      
      print (df)
        Column1 Column2 Column3
      0     aaa     bbb     ccc
      1    none     ddd     ggg
      
      print (df['Column1'] == "none")
      0    False
      1     True
      Name: Column1, dtype: bool
      
      print (df[df['Column1'] == "none"])
        Column1 Column2 Column3
      1    none     ddd     ggg
      

      如果值在字符串的开头包含空格,请使用str.strip

      import pandas as pd
      
      df = pd.DataFrame({'Column2': {0: ' bbb', 1: ' ddd'}, 
                         'Column1': {0: ' aaa', 1: ' none'}, 
                         'Column3': {0: ' ccc', 1: ' ggg'}})
      
      print (df)
        Column1 Column2 Column3
      0     aaa     bbb     ccc
      1    none     ddd     ggg
      
      print (df['Column1'].str.strip() == "none")
      0    False
      1     True
      Name: Column1, dtype: bool
      
      print (df[df['Column1'].str.strip() == "none"])
        Column1 Column2 Column3
      1    none     ddd     ggg
      

      【讨论】:

        【解决方案4】:

        这是另一种方法,它首先手动去除空白,然后将处理后的文件内容提供给 pandas。

        import pandas as pd
        from io import StringIO
        
        # First strip out the whitespace
        contents = open('Input.txt').read().splitlines()
        contents = "\n".join([",".join([x.strip() for x in y.split(",")]) for y in contents])
        
        # Convert to a stringIO to feed to pandas
        df = pd.read_csv(StringIO(unicode(contents, 'utf-8')))
        
        print df[df['Column1'] == "none"]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-06
          • 1970-01-01
          • 1970-01-01
          • 2018-01-07
          • 1970-01-01
          • 1970-01-01
          • 2016-07-08
          • 2021-06-07
          相关资源
          最近更新 更多