我有一個 CSV 檔案,它具有以下結構(在 Excel 上查看時),其中具有占據單個單元格的唯一部分標題(字串),后跟一個包含列名稱和資料的塊。
這種格式在整個 Excel 電子表格中重復出現。請注意,每個部分都有不同的列名稱和編號,以及可變的行數。例子:
Daily Statements
Date Desc Costumer ID Phone Status
12/21/21 aaa 1 123-123-1231 OK
12/21/21 aaa 2 333-123-1231 OK
12/21/21 bbb 3 222-123-1231 OK
12/21/21 bbb 3 444-123-1231 OK
<===== one empty row separates sections
Account History
Date Time Type Ref # Balance
12/21/21 1:00:00 BAL 456 $0.01
12/21/21 1:00:00 BAL 445 $0.01
12/21/21 1:00:00 BAL 645 $0.01
<===== one empty row separates sections
Order History
ID Date Ref #
1 12/21/21 777
2 12/21/21 888
3 12/21/21 999
4 12/21/21 9995
我的目標是僅提取帳戶歷史記錄中的行:
Date Time Type Ref # Balance
12/21/21 1:00:00 BAL 456 $0.01
12/21/21 1:00:00 BAL 445 $0.01
12/21/21 1:00:00 BAL 645 $0.01
但是,我無法找到適用于 Pandas 的方法,因為我需要使用字串“Account History”作為錨點來指示感興趣的行。
你知道這是如何實作的嗎?
uj5u.com熱心網友回復:
我沒有看到僅使用 Pandas 就能做到這一點的直接方法。為什么不首先將檔案作為文本檔案讀取以查找感興趣的行,然后才使用 Pandas 僅匯入這些行?
with open(file, 'r') as f:
# read until the line "Account History"
for line_n, line_content in enumerate(f):
if "Account History" in line_content:
break
start_row = line_n 1
# continue reading, and find the following new line
for line_n, line_content in enumerate(f):
if line_content == '\n':
break
tab_size = line_n - 1
# import the dataframe, just from the target lines
df = pd.read_csv(file, skiprows=start_row, nrows=tab_size)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/396391.html
