[英]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.