【问题标题】:How to read and print rows with common data in CSV using pandas如何使用 pandas 在 CSV 中读取和打印具有常见数据的行
【发布时间】:2020-06-11 01:47:43
【问题描述】:

我正在使用以下代码使用 python 和 pandas 从 csv 读取特定行。 但是当我想打印常见数据、文本行时,我被卡住了。 我想将包含订单代码的行打印为 00157B。 我正在使用的场景和附加代码的 PFA 屏幕截图。

rows = pd.read_csv('SampData.csv', skiprows=[1,3])
print(rows.head())

【问题讨论】:

  • 目前还不清楚您要做什么。您有一个包含许多订单代码的 CSV……它们是按行唯一的吗?或者是否有多个您想查找具有相同订单代码的行条目?不清楚您的描述中的“”是什么。你为什么用熊猫?
  • 嘿 Jeff,我有 94​​5 行和多列的庞大数据。我只想搜索一个名称(文本或术语),例如 00157B 或 并仅打印这些行。我想搜索 (这是我拥有的数据格式)然后我应该得到所有包含 的行来显示。它可能有不同的逗号分隔值,但我需要打印整行。然后我想创建这些常见行的另一个 CSV。这是要求。
  • 好的。您可以为此使用pandas,但如果您想要做的只是在 csv 中定位行并对其进行处理,这可能比需要的更困难。 csv 阅读器在这方面做得很好。
  • 谢谢!但是你能告诉我如何找到其中包含公共数据的行吗?

标签: python pandas csv data-science rows


【解决方案1】:

您可以尝试其中一种 -

如果您想对 OrderCode 列中的整个术语进行比较(例如 00157B):

 filtered = rows[rows['OrderCode'] == '00157B'].reset_index(drop=True)
 filtered.to_csv('output.csv', index=False)

如果您想通过 OrderCode 列中的部分术语进行比较(例如 OrderCodes< Test 1 > ):

filtered = rows[rows['OrderCode'].str.contains('< Test 1 >')].reset_index(drop=True)
filtered.to_csv('output.csv', index=False)

【讨论】:

    【解决方案2】:

    使用 Pandas 可以做到这一点,但它是矫枉过正的。我只推荐csv 模块,它很容易使用并且有很多在线文档。这是一个玩具示例,我认为它几乎可以满足您的需求

    data.csv 文件:

    Order Number, Item, quantity
    76XY, Cheese, 3
    88TG, Broccoli, 44
    76XY, Cookies, 1000
    98UU, Coke, 1
    

    一个操作 2 个 csv 文件的短文件:

    import csv
    input_file = 'data.csv'
    output_file = 'found_orders.csv'
    magic_order = '76XY'
    
    with open(output_file, 'w') as target:
        target_writer = csv.writer(target)
    
        # open the source
        with open(input_file, 'r') as source:
            source_reader = csv.reader(source)
            # now both are "open and ready"
            # get the header from the first read of the source
            header = source_reader.__next__()
    
            # write to the modified file
            target_writer.writerow(header)
    
            # now use loop to loop through all rows and look for the order number
            # if found, print it to console and write it out to new csv
            # some extra print statements to see what is going on.  Recall
            # when csv reader reads a row, it reads it in as a list
            for row in source_reader:
                print('just read row: ', row)
                order = row[0]  # the position in the list of the order
                if order == magic_order:
                    print('it matches the magic order')
                    target_writer.writerow(row)
                else:
                    print('not a match')
                print()
    
    # if you use the 'with' command structure, it will close the files automatically
    print('done')
    

    输出(到控制台)。输出文件是您希望看到的。:

    just read row:  ['76XY', ' Cheese', ' 3']
    it matches the magic order
    
    just read row:  ['88TG', ' Broccoli', ' 44']
    not a match
    
    just read row:  ['76XY', ' Cookies', ' 1000']
    it matches the magic order
    
    just read row:  ['98UU', ' Coke', ' 1']
    not a match
    
    done
    [Finished in 0.0s]
    

    【讨论】:

    • 好的。如果它适合你,请接受上面的答案。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2018-10-09
    • 2019-08-03
    • 2018-10-21
    相关资源
    最近更新 更多