如何在循环期间向数据框添加新的原始数据并满足特定条件?

乘风 python 184

原文标题how to add new raw to the data frame during a loop and a certain condition is met?

我想在迭代达到具有“总费用”的原始数据时添加一个新行。仅供参考:如代码所示,第 1 列是必须执行的位置。

python
for row in df.itertuples():
    row[1] == 'Total Charges'

这就是数据的样子,我需要用一行分隔它,就在总费用下面

原文链接:https://stackoverflow.com//questions/71464193/how-to-add-new-raw-to-the-data-frame-during-a-loop-and-a-certain-condition-is-me

回复

我来回复
  • Dennis的头像
    Dennis 评论

    希望我理解正确(我使用了您提供的数据示例)。迭代行并搜索Total Charges。然后用pandas.concat()

    import pandas as pd
    
    df = pd.DataFrame({'column1': ['data_row_1', 'data_row_2', 'Total Charges', 'data_row_3', 'data_row_4'], 'column2': range(1, 6)})
    
    for index, row in df.iterrows():
        if row['column1'] == 'Total Charges':
            df_before = df.iloc[:index+1]
            df_after = df.iloc[index+1:]
            new_row = pd.DataFrame({'column1': ['new_data_1'], 'column2': ['new_data_2']})
            new_df = pd.concat([df_before, new_row, df_after], ignore_index=True)
            break
    
    print(new_df)
    

    输出:

             column1     column2
    0     data_row_1           1
    1     data_row_2           2
    2  Total Charges           3
    3     new_data_1  new_data_2
    4     data_row_3           4
    5     data_row_4           5
    
    2年前 0条评论
  • keramat的头像
    keramat 评论

    采用:

    import pandas as pd
    s = list(range(3))
    s.append('Total Charges')
    s.extend(list(range(3)))
    df = pd.DataFrame({'c1': s, 'c2': range(7)})
    ind = df[df['c1']=='Total Charges'].index
    df.loc[ind[0]+.5]='',''
    df = df.sort_index().reset_index()
    del df['index']
    

    输出:

    enter image description here

    2年前 0条评论