简体   繁体   English

基于过滤器添加新列并添加来自另一个 DataFrame 的值

[英]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:基于过滤器添加新列并添加来自另一个 DataFrame 的值:

I have two DataFrames as follows: infra_df:-我有两个 DataFrames 如下:infra_df:-

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

measures_df:-措施_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:-现在我想在 Name 为 net 和 NAN 的其他地方添加从 measure_df 到 infra_df 的列:-

result_df:-结果_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:如果 measure_df 的长度与infra_df中的net数量相同,请使用:

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:以 net 为 Name 的示例索引:

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

We concat with measures_df with a modified index:我们将measures_df与修改后的索引连接起来:

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.我还添加了 iloc,以防 measure_df 中的行数多于 intra_df 中的 net 行数。

.dropna() drops all the nan rows if that is what you need. .dropna() 如果这是您需要的,则删除所有 nan 行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:根据列中的值向DataFrame添加新列 - Pandas: Add new columns to DataFrame based on values in columns 将新列添加到 dataframe 这是基于重复日期时间索引的前一个月的另一列的值,其他列作为标识符 - Add new column to dataframe that is another column's values from the month before based repeating datetime index with other columns as identifiers 根据另一个数据帧将列添加到 Pandas 数据帧并将值设置为零 - Add columns to Pandas dataframe based on another dataframe and set values to zero 将列添加到 DataFrame 中,特定列的差异基于另一列的值 - Add columns to DataFrame with difference of specific columns based on values of another column 如何根据第一个数据帧列中的值从另一个数据帧添加新列? - How to add new column from another dataframe based on values in column of first dataframe? 如何将新列添加到 dataframe,其值取自另一个 dataframe? - How to add new columns to dataframe with value taken from another dataframe? 如何根据列值向 dataframe 添加新列? - How to add new columns to the dataframe based on the column values? Pyspark-根据来自不同数据框的值向数据框添加列 - Pyspark - add columns to dataframe based on values from different dataframe Pandas 根据另一个数据框中 2 列的值过滤行 - Pandas filter rows based on values from 2 columns in another dataframe 根据来自另一个 DataFrame 的信息,将新列添加到 DataFrame - Add new column to DataFrame based on information from another DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM