遍历行然后遍历列以构建ColHeader:ColValue字符串python

时间:2019-07-26 11:09:35

标签: python pandas

我有一个不同值的数据框,我需要遍历每一行以建立一个列名和值的字符串,格式如下:

{col1Name:row1Value
 col2Name:row1Value
 col3Name:row1Value},
{col1Name:row2Value
 col2Name:row2Value
 col3Name:row2Value},....

这必须是动态的,以便无论数据框中有多少列都可以应用。

我设法遍历行然后遍历列,但是现在不确定如何构建字符串。

for index, row in distinct_features.iterrows():
    for index, column in row.iteritems():
        print(column)

以下是数据框的示例:

  label hoverBackgroundColor backgroundColor
0    AF                 #FFF            #000
1    FR                 #ASD            #000
2    IT                 #DJH            #000
3    MC                 #NHG            #000
4    PA                 #GYA            #000
5    RP                 #BBH            #000
6    WT                 #ERT            #000

2 个答案:

答案 0 :(得分:1)

这是一种实现方法:

OP <- function(DF, dan){
  DF$dangerous <- NA
  for (i in 1:nrow(DF)){
    for(j in 1:length(dan)){
      if (DF$des[i]==dan[j]) DF$dangerous[i] <- 1
    } 
  }
  DF
}

Carles <- function(DF, dan){
  DF$dangerous<-ifelse(DF$des %in% dan, 1, NA)
  DF
}

arg0naut91_1 <- function(DF, dan){
  DF$dangerous <- NA
  transform(DF, dangerous = replace(dangerous, des %in% dan, 1))
}

arg0naut91_2 <- function(DF, dan){
  DF$dangerous <- NA
  DF$dangerous[DF$des %in% dan] <- 1
  DF
}

Rui <- function(DF, dan){
  DF$dangerous <- c(1, NA)[(DF$des %in% dan) + 1]
  DF
}

library(microbenchmark)

mb <- microbenchmark(
  OP = OP(d, dan),
  Carles = Carles(d, dan),
  Rui = Rui(d, dan),
  arg0naut91_1 = arg0naut91_1(d, dan),
  arg0naut91_2 = arg0naut91_2(d, dan)
)
print(mb, order = "median")
#Unit: microseconds
#         expr     min       lq      mean   median       uq       max neval cld
#          Rui  22.623  25.1865  82.73746  27.2510  31.6630  5441.491   100  a 
#       Carles  31.740  34.4120  76.82339  36.9385  42.1760  3753.407   100  a 
# arg0naut91_2  34.131  36.7140  89.10827  39.5925  46.6930  4577.938   100  a 
# arg0naut91_1 226.237 230.1020 296.23198 234.6225 243.3040  4847.553   100  a 
#           OP 757.831 770.1875 926.88995 781.5630 818.2745 10992.040   100   b



e <- d
for(i in 1:10) e <- rbind(e, e)

mb2 <- microbenchmark(
  OP = OP(e, dan),
  Carles = Carles(e, dan),
  Rui = Rui(e, dan),
  arg0naut91_1 = arg0naut91_1(e, dan),
  arg0naut91_2 = arg0naut91_2(e, dan),
  times = 10
)
print(mb2, order = "median")
#Unit: microseconds
#         expr        min         lq        mean      median         uq        max neval cld
#          Rui    291.090    294.690    346.3638    298.9580    301.238    776.769    10  a 
# arg0naut91_2    288.123    292.236    312.6684    311.2435    314.495    388.212    10  a 
#       Carles    427.500    430.120    447.7170    450.2570    453.884    480.424    10  a 
# arg0naut91_1    513.059    517.822    611.0255    666.7095    670.059    688.023    10  a 
#           OP 898781.320 909717.469 911988.3906 914269.7245 916975.858 919223.886    10   b

输出您提供的数据样本:

columns = df.columns
labels = df.label
hover = df.hoverBackgroundColor
background = df.backgroundColor

s = ''
for row in range(len(labels)):
    s += "{{{} : {} \n {} : {}\n {} : {} }}\n".format(columns[0],
                                                 labels[row], columns[1], hover[row], columns[2], background[row])

答案 1 :(得分:1)

您可以这样做:

 df_dict = distinct_features.to_dict('records')