[英]Add new columns and add values from another DataFrame based on a filter
基于过滤器添加新列并添加来自另一个 DataFrame 的值:
我有两个 DataFrames 如下:infra_df:-
Name time
net 8am
stat 8am
net 8am
net 8am
sig 8am
net 8am
措施_df:-
tcp_time. tcp_wait
12 33
22 11
23 32
34 11
现在我想在 Name 为 net 和 NAN 的其他地方添加从 measure_df 到 infra_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
如果 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
以 net 为 Name 的示例索引:
idx = intra_df.loc[intra_df["Name"].eq("net")].index
我们将measures_df与修改后的索引连接起来:
intra_df = pd.concat([intra_df, measures_df.iloc[:len(idx),:].set_index(idx)], axis=1)
我还添加了 iloc,以防 measure_df 中的行数多于 intra_df 中的 net 行数。
.dropna() 如果这是您需要的,则删除所有 nan 行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.