遍历熊猫数据框

时间:2019-11-24 02:15:23

标签: python pandas dataframe for-loop

我有以下代码:

数据的列名称为col_name。

data = pd.csv_read("somedata.csv")
for row in data:
     print(row)

这仅打印col_name的列名。为什么不遍历整个列?

2 个答案:

答案 0 :(得分:1)

\documentclass{article}

\usepackage[margin=1in]{geometry}% Just for this example
\usepackage[table]{xcolor}
\usepackage{booktabs,collcell,xfp}

\newcommand*{\ang}[2]{#1^\circ}
\newcommand*{\MinNumber}{-0.01}%
\newcommand*{\MaxNumber}{1.2}%
\newcommand*{\MidNumber}{0.8}%

\newcommand{\test}[2]{\ifdim#1pt>\MidNumber pt\textcolor{gray!70}{#1}\else #1\fi}

\newcommand{\ApplyGradient}[2]{\centering %
  \edef\x{\noexpand\cellcolor{black!\fpeval{100*(#1-\MinNumber)/(\MaxNumber-\MinNumber)}}}\x\test{#1}
}
\newcolumntype{R}{>{\collectcell\ApplyGradient}p{1.1cm}<{\endcollectcell}}
\newcolumntype{S}{>{\collectcell\ApplyGradient}p{0.6cm}<{\endcollectcell}}
%

\begin{document}

\begin{table}
  \centering
  \begin{tabular}{
    >{\centering}p{1.3cm} |
    >{\centering}p{0.9cm} |
    *{5}{R}
  }
    \multicolumn{2}{c}{testing in$\rightarrow$} & 
      \multicolumn{1}{c}{$\ang{40}$} & 
      \multicolumn{1}{c}{$\ang{20}$} & 
      \multicolumn{1}{c}{$\ang{0}$}  & 
      \multicolumn{1}{c}{$-\ang{20}$} & 
      \multicolumn{1}{c}{$-\ang{40}$} \\
    \midrule
                & $\ang{40}$  & 0.52 & 0.44 & 0.27 & 0.10 & -0.01 \\
                & $\ang{20}$  & 0.47 & 0.49 & 0.35 & 0.19 &  0.07 \\
    spider9     & $\ang{0}$   & 0.25 & 0.47 & 0.63 & 0.46 &  0.24 \\
                & $-\ang{20}$ & 0.05 & 0.17 & 0.35 & 0.55 &  0.54 \\
                & $-\ang{40}$ & 0.0  & 0.05 & 0.21 & 0.47 &  0.77 \\
    \midrule
                & $\ang{40}$  & 0.75 & 0.69 & 0.40 & 0.17 &  0.04 \\
                & $\ang{20}$  & 0.72 & 0.82 & 0.59 & 0.30 &  0.10 \\
    gecko7      & $\ang{0}$   & 0.31 & 0.65 & 1.06 & 0.96 &  0.56 \\
                & $-\ang{20}$ & 0.16 & 0.45 & 0.86 & 1.18 &  0.98 \\
                & $-\ang{40}$ & 0.03 & 0.15 & 0.40 & 0.74 &  0.96 \\
    \midrule
                & $\ang{40}$  & 1.09 & 0.75 & 0.39 & 0.14 &  0.02 \\
                & $\ang{20}$  & 0.54 & 0.55 & 0.33 & 0.15 &  0.04 \\
    babyA       & $\ang{0}$   & 0.29 & 0.58 & 0.88 & 0.74 &  0.42 \\
                & $-\ang{20}$ & 0.18 & 0.47 & 0.85 & 1.09 &  0.71 \\
                & $-\ang{40}$ & 0.02 & 0.11 & 0.33 & 0.65 &  0.91 \\
    \bottomrule            
  \end{tabular}
  \hspace{4mm}
  \begin{tabular}{ S }
    0.0 \\ 0.1 \\ 0.2 \\ 0.3 \\ 0.4 \\ 0.5 \\
    0.6 \\ 0.7 \\ 0.8 \\ 0.9 \\ 1.0 \\ 1.1 \\
    1.2
  \end{tabular}

  \caption{Robustness of the top learned controllers for different directions in the real world. 
    The data shows the fitness that the top controllers of three robots in~5 directions (the second column) perform in another directions (the first row). 
    The fitness value in a cell is averaged over three controller and three repetitions for each controller. 
    The dark and light color represent the high and low fitness value respectively.}
\end{table}

\end{document}

for column in data:
    print(data[column])

答案 1 :(得分:0)

for key,value in data.iteritems():
     print(key,value)
     print()