Adding a new row to a dataframe in pandas for every iteration

2024/10/12 14:16:47

Adding a new row to a dataframe with correct mapping in pandas

Something similar to the above question.

      carrier_plan_identifier           ...            hios_issuer_identifier
1                        AUSK           ...                           99806.0
2                        AUSM           ...                           99806.0
3                        AUSN           ...                           99806.0
4                        AUSS           ...                           99806.0
5                        AUST           ...                           99806.0

I need to pick multiple columns, lets say carrier_plan_identifier, wellthie_issuer_identifier and hios_issuer_identifier.

With these 3 columns I need to run a select query, something like ,

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

I need to add id column back to my existing dataframe

Currently, I am doing something like this,

for index, frame in df_with_servicearea.iterrows():if frame['service_area_id'] and frame['issuer_id']:# reading from medical plans tablemedical_plan_id = getmodeldata.get_medicalplans(sess, frame['issuer_id'], frame['hios_plan_identifier'], frame['plan_year'],frame['group_or_individual_plan_type'])frame['medical_plan_id'] = medical_plan_iddf_with_servicearea.append(frame)

when I do this,frame['medical_plan_id'] = medical_plan_id , nothing is added. But when I do df_with_servicearea['medical_plan_id'] = medical_plan_id only the last value of the loop is added to all the rows. I am not sure if this is the correct way to do this.

Update -:

After using , I am getting 4 rows , instead of 2 rows which should be there.

df_with_servicearea = df_with_servicearea.append(frame)wellthie_issuer_identifier       ...       medical_plan_id
0                   UHC99806       ...                   NaN
1                   UHC99806       ...                   NaN
0                   UHC99806       ...              879519.0
1                   UHC99806       ...              879520.0

Update 2 - Implemented based on Mayank's answer- Hi Mayank , Is something like this you are suggesting.

for index, frame in df_with_servicearea.iterrows():

    if frame['service_area_id'] and frame['issuer_id']:# reading from medical plans tabledf_new = getmodeldata.get_medicalplans(sess, frame['issuer_id'], frame['hios_plan_identifier'], frame['plan_year'],frame['group_or_individual_plan_type'])df_new.columns = ['medical_plan_id', 'issuer_id', 'hios_plan_identifier', 'plan_year','group_or_individual_plan_type']new_df = pd.merge(df_with_servicearea, df_new, on=['issuer_id', 'hios_plan_identifier', 'plan_year', 'group_or_individual_plan_type'], how='left')print new_df

my get_medicalplans function where I am calling the select query.

def get_medicalplans(self,sess, issuerid, hios_plan_identifier, plan_year, group_or_individual_plan_type):try:medical_plan = sess.query(MedicalPlan.id, MedicalPlan.issuer_id, MedicalPlan.hios_plan_identifier,MedicalPlan.plan_year, MedicalPlan.group_or_individual_plan_type).filter(MedicalPlan.issuer_id == issuerid,MedicalPlan.hios_plan_identifier == hios_plan_identifier,MedicalPlan.plan_year == plan_year,MedicalPlan.group_or_individual_plan_type == group_or_individual_plan_type)sess.commit()return pd.read_sql(medical_plan.statement, medical_plan.session.bind) 
Answer

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']and row['issuer_id']else np.nan)
https://en.xdnf.cn/q/118187.html

Related Q&A

Twisted client protocol - attaching an interface frontend

I am following the tutorial on writing a client/server pair in Twisted located here:http://twistedmatrix.com/documents/current/core/howto/clients.htmlI have everything working for the communication of …

Get query string as function parameters on flask

Is there a way to get query string as function parameters on flask? For example, the request will be like this.http://localhost:5000/user?age=15&gender=MaleAnd hope the code similar to this.@app.…

cython.parallel cannot see the difference in speed

I tried to use cython.parallel prange. I can only see two cores 50% being used. How can I make use of all the cores. i.e. send the loops to the cores simultaneously sharing the arrays, volume and mc_vo…

Is it possible (how) to add a spot color to pdf from matplotlib?

I am creating a chart which has to use (multiple) spot colors. This color could be one that is neither accessible from RGB nor CMYK. Is there a possibility to specify a spot color for a line in matplot…

Separate keywords and @ mentions from dataset

I have a huge set of data which has several columns and about 10k rows in more than 100 csv files, for now I am concerned about only one column with message format and from them I want to extract two p…

Kivy class in .py and .kv interaction 2

Follow up from Kivy class in .py and .kv interaction , but more complex. Here is the full code of what Im writing: The data/screens/learnkanji_want.kv has how I want the code to be, but I dont fully un…

How to centre an image in pygame? [duplicate]

This question already has an answer here:How to center an image in the middle of the window in pygame?(1 answer)Closed 1 year ago.I am using python 2.7, I would like to know if there is a way to centr…

widget in sub-window update with real-time data in tkinter python

Ive tried using the after/time.sleep to update the treeview, but it is not working with the mainloop. My questions are: How can I update the treeview widget with real-time data? And is there a way th…

Changing for loop to while loop

Wondering how would the following for loop be changed to while loop. While having the same output.for i in range(0,20, 4):print(i)

How to dynamically resize label in kivy without size attribute

So, I get that you can usually just use self(=)(:)texture_size (py,kv) but all of my widgets are either based on screen(root only) or size_hint. I am doing this on purpose for a cross-platform GUI. I o…