简体   繁体   中英

Add new columns and add values from another DataFrame based on a filter

Add new columns and add values from another DataFrame based on a filter:

I have two DataFrames as follows: infra_df:-

    Name   time  
    net    8am
    stat   8am
    net    8am
    net    8am
    sig    8am
    net    8am

measures_df:-

    tcp_time.  tcp_wait   
    12         33
    22         11
    23         32
    34         11

    

Now I want to add columns from measures_df to infra_df for rows wherever Name is net and NAN everywhere else:-

result_df:-

    Name   time   tcp_time   tcp_wait
    net    8am    12         33
    stat   8am    NAN        NAN
    net    8am    22         11
    net    8am    23         32
    sig    8am    NAN        NAN
    net    8am    34         11

If length of measures_df is same like number of net values in infra_df use:

m = infra_df['Name'].eq('net')
df = pd.concat([infra_df, measures_df.set_index(m.index[m])], axis=1)
print (df)
   Name time  tcp_time.  tcp_wait
0   net  8am       12.0      33.0
1  stat  8am        NaN       NaN
2   net  8am       22.0      11.0
3   net  8am       23.0      32.0
4   sig  8am        NaN       NaN
5   net  8am       34.0      11.0

The index of the examples with net as Name:

idx = intra_df.loc[intra_df["Name"].eq("net")].index

We concat with measures_df with a modified index:

intra_df = pd.concat([intra_df, measures_df.iloc[:len(idx),:].set_index(idx)], axis=1)

I also added iloc in case there would be more rows in measures_df than net in intra_df.

.dropna() drops all the nan rows if that is what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM