python - How to search for rows with same column value and insert row if it is not present? -


i have following dataset:

  cvso band   period       pvalue 1    4    r 1.063372 5.383864e-03 2    4    v 1.652512 1.543246e-17 3   27    v 6.114795 2.174296e-12 4   24    7.163776 1.014593e-17 5   24    r 7.164236 0.000000e+00 6   24    v 7.171452 3.342914e-14 

for each value in first column, search whether band i, r , v exist. example, in dataset, 4 has band r , v, whereas 24 has 3 bands. if 1 or more of bands not exist, add na row such following output:

  cvso band   period       pvalue 1    4          na           na 2    4    r 1.063372 5.383864e-03 3    4    v 1.652512 1.543246e-17 4   27          na           na 5   27    r       na           na 6   27    v 6.114795 2.174296e-12 7   24    7.163776 1.014593e-17 8   24    r 7.164236 0.000000e+00 9   24    v 7.171452 3.342914e-14 

(using r) here's possible data.table solution

library(data.table) lookup <- c("i", "r", "v") res <- setdt(df)[, .sd[match(lookup, band)] , = cvso][, band := lookup] res #    cvso band   period       pvalue # 1:    4          na           na # 2:    4    r 1.063372 5.383864e-03 # 3:    4    v 1.652512 1.543246e-17 # 4:   27          na           na # 5:   27    r       na           na # 6:   27    v 6.114795 2.174296e-12 # 7:   24    7.163776 1.014593e-17 # 8:   24    r 7.164236 0.000000e+00 # 9:   24    v 7.171452 3.342914e-14 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -