Skip to content Skip to sidebar Skip to footer

Adding A New Row To A Dataframe In Pandas For Every Iteration

Adding a new row to a dataframe with correct mapping in pandas Something similar to the above question. carrier_plan_identifier ... hios_issuer_identifie

Solution 1:

The simplest solution to your issue is to change last row into:

df_with_servicearea = df_with_servicearea.append(frame)

However, if you want to add new column, use:

df_with_servicearea['medical_plan_id'] = df_with_servicearea.apply(
    lambda row:
    getmodeldata.get_medicalplans(sess,
                                  row['issuer_id'],
                                  row['hios_plan_identifier'],
                                  row['plan_year'],
                                  row['group_or_individual_plan_type']
                                  )
    if row['service_area_id']
    androw['issuer_id']
    else np.nan)

Solution 2:

Try this:

Considering that you want to update the original df based on the below 3 cols:

1.) Tweak the query which you are firing on DB to include columns:carrier_plan_identifier, wellthie_issuer_identifier and hios_issuer_identifier in the select clause.

select id,carrier_plan_identifier, wellthie_issuer_identifier,hios_issuer_identifier from table_name where carrier_plan_identifier = 'something'and wellthie_issuer_identifier = 'something'and hios_issuer_identifier = 'something'

2.) Create a dataframe for the above results.

df = pd.DataFrame(cur.fetchall())

3.) Now above df has id column with the 3 other columns. Now, merge this df with the original_df based on columns : carrier_plan_identifier, wellthie_issuer_identifier and hios_issuer_identifier

original_df = pd.merge(original_df,df, on=['carrier_plan_identifier','wellthie_issuer_identifier','hios_issuer_identifier'],how='outer')

Changed left join to Outer join.

So, you have to understand what's happening here. I am joining query dataframe(df) with the original df on columns carrier_plan_identifier, wellthie_issuer_identifier and hios_issuer_identifier and appending the id column as it was not present. Wherever a match is found, id column's value from df will be copied to original_df and in case of no match, id column will have NaN. You don't have to use any loops. Just try out my code.

This will add id column to original_df for all rows which match. For rows which don't find a match will have id as Nan.

You can replace Nan with any value like below:

original_df = original_df.fillna("")

Let me know if this helps.

Post a Comment for "Adding A New Row To A Dataframe In Pandas For Every Iteration"