根据条件将数据帧拆分为多个表

问题描述 投票:0回答:1

我正在尝试将一个巨大的数据框分成几个表,但取决于条件

import pandas as pd

Thank you so much in advance.

Best,
Felipe
df=pd.DataFrame({'a':['450','455',None,'560','350',None,'580','950'],
'b':['10',None,'15','12','15',None,'18','15']})

print(df)

currentRow = None
lastRow = None
tables = {}  # Dictionary to store the tables

tableNum = 0 # Counter for table numbers

for index, row in df.iterrows():
currentRow = row['a']

    if currentRow is not None and lastRow is None:
               tables[tableNum] = pd.DataFrame(columns=df.columns)
               tables[tableNum].loc[index] = row
    
    
    
    elif currentRow is not None and lastRow is not None:
            tables[tableNum].loc[index] = row
    
    
    
    elif currentRow is None and lastRow is not None:
        tables[tableNum].loc[index] = row

lastRow = currentRow

print('tables:', tables)

但我不知道这段代码有什么问题。

我尝试过 def case, if else 但我不知道如何检测问题出在哪里

python dataframe split conditional-statements
1个回答
0
投票

您的代码中存在一些缩进问题。不过我可以运行你的代码并找到解决方案:

    import pandas as pd

df = pd.DataFrame({'a': ['450', '455', None, '560', '350', None, '580', '950'],
                   'b': ['10', None, '15', '12', '15', None, '18', '15']})

print(df)

currentRow = None
lastRow = None
tables = {}  # Dictionary to store the tables
tableNum = 0  # Counter for table numbers

for index, row in df.iterrows():
    currentRow = row['a']

    if currentRow is not None and lastRow is None:
        tableNum += 1  # Increment the table number
        tables[tableNum] = pd.DataFrame(columns=df.columns)
        tables[tableNum].loc[index] = row

    elif currentRow is not None and lastRow is not None:
        tables[tableNum].loc[index] = row

    elif currentRow is None and lastRow is not None:
        tables[tableNum].loc[index] = row

    lastRow = currentRow

# Print each table
for key, table in tables.items():
    print(f"Table {key}:")
    print(table)
    print("\n")

所做的更改:

  1. 创建新表时增加tableNum。

  2. 调整缩进以获得更好的可读性。

  3. 删除了不必要的 def case 语句。

  4. 添加了打印每个表格以进行验证的代码。

© www.soinside.com 2019 - 2024. All rights reserved.