<p>Here is one potential approach:</p>
<p>Example data:</p>
<pre class="lang-r prettyprint-override"><code>expr1 &lt;- structure(list(GeneA_KO = c(0.0756383119639074, -0.162841965682723, -0.110703100204517, -0.457154616239399, -0.505815193885352, 0.366072775033448, 0.288650458935083, -0.0287646498640309, -0.690732824311905, 0.157366122620995, -1.29451546737684, 0.124167425460344, -0.168045313861751, 0.0186504943306465, -0.495880827400706, -0.206668627339351, -0.148609490335447, 0.066844529089804), GeneB_KO = c(-0.870157285953621, -0.35900872537626, -0.990406888128838, -0.120656981579292, -1.41427631791049, -0.866421095836781, -0.816936112146099, -0.894597236248507, -1.00084584207823, -0.850754488133456, -1.05314239761653, -1.07580675428372, -1.24708324665688, -1.04098460460899, -0.232388675985086, -1.48513703581865, -1.48373348870346, -1.76484058374354)), class = &quot;data.frame&quot;, row.names = c(&quot;ModelM&quot;, &quot;ModelO&quot;, &quot;ModelS&quot;, &quot;ModelA&quot;, &quot;ModelI&quot;, &quot;ModelC&quot;, &quot;ModelT&quot;, &quot;ModelH&quot;, &quot;ModelE&quot;, &quot;ModelR&quot;, &quot;ModelA2&quot;, &quot;ModelP&quot;, &quot;ModelE2&quot;, &quot;ModelU&quot;, &quot;ModelT2&quot;, &quot;ModelC2&quot;, &quot;ModelS2&quot;, &quot;ModelX&quot;))
mut &lt;- structure(list(ModelM = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelO = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelS = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelA = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelI = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelC = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelT = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L), ModelH = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L), ModelE = c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L), ModelR = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), ModelA2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelP = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L), ModelE2 = c(1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L), ModelU = c(1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L), ModelT2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelI2 = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelC2 = c(1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L),     ModelS2 = c(0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L)), class = &quot;data.frame&quot;, row.names = c(&quot;Gene1_mut&quot;, &quot;Gene2_mut&quot;, &quot;Gene3_mut&quot;, &quot;Gene4_mut&quot;, &quot;Gene5_mut&quot;, &quot;Gene6_mut&quot;, &quot;Gene7_mut&quot;, &quot;Gene8_mut&quot;, &quot;Gene9_mut&quot;, &quot;Gene10_mut&quot;))
</code></pre>
<hr />
<p>R code:</p>
<pre class="lang-r prettyprint-override"><code>my_data=cbind(mut[,1],expr1[1:10,1])
colnames(my_data)=c(&quot;mut&quot;,&quot;expr1&quot;)
res.aov &lt;- aov(expr1 ~ mut, data = as.data.frame(my_data))
summary(res.aov)
#&gt;             Df Sum Sq Mean Sq F value Pr(&gt;F)
#&gt; mut          1 0.1364  0.1364   1.114  0.322
#&gt; Residuals    8 0.9789  0.1224

for (j in 1:nrow(mut)) {
  for(i in 1:ncol(expr1)) {
    my_data=as.data.frame(cbind(mut[,j],expr1[1:10,i]))
    print(broom::tidy(aov(my_data[,2] ~ my_data[,1], data = my_data)))
  }
}
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#&gt; 2 Residuals        8 0.979  0.122     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#&gt; 2 Residuals        8 0.587 0.0733     NA    NA     
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#&gt; 2 Residuals        8 0.979  0.122     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#&gt; 2 Residuals        8 0.587 0.0733     NA    NA     
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#&gt; 2 Residuals        8 0.979  0.122     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#&gt; 2 Residuals        8 0.587 0.0733     NA    NA     
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#&gt; 2 Residuals        8 0.979  0.122     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#&gt; 2 Residuals        8 0.587 0.0733     NA    NA     
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#&gt; 2 Residuals        8 0.979  0.122     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#&gt; 2 Residuals        8 0.587 0.0733     NA    NA     
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#&gt; 2 Residuals        8 0.979  0.122     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#&gt; 2 Residuals        8 0.587 0.0733     NA    NA     
#&gt; # A tibble: 2 × 6
#&gt;   term            df  sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.0612 0.0612     0.465   0.515
#&gt; 2 Residuals        8 1.05   0.132     NA      NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df  sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.0637 0.0637     0.479   0.508
#&gt; 2 Residuals        8 1.06   0.133     NA      NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.246  0.246      2.26   0.171
#&gt; 2 Residuals        8 0.869  0.109     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.313  0.313      3.07   0.118
#&gt; 2 Residuals        8 0.815  0.102     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df   sumsq  meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.00108 0.00108   0.00777   0.932
#&gt; 2 Residuals        8 1.11    0.139    NA        NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.220  0.220      1.94   0.201
#&gt; 2 Residuals        8 0.907  0.113     NA     NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df  sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.0529 0.0529     0.398   0.546
#&gt; 2 Residuals        8 1.06   0.133     NA      NA    
#&gt; # A tibble: 2 × 6
#&gt;   term            df  sumsq meansq statistic p.value
#&gt;   &lt;chr&gt;        &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
#&gt; 1 my_data[, 1]     1 0.0901 0.0901     0.695   0.429
#&gt; 2 Residuals        8 1.04   0.130     NA      NA

# write.csv(mut, &quot;mut.csv&quot;, quote = FALSE, row.names = FALSE)
# write.csv(expr1[1:10,], &quot;expr1.csv&quot;, quote = FALSE, row.names = FALSE)
</code></pre>
<p><sup>Created on 2023-09-19 with <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex v2.0.2</a></sup></p>
<hr />
<p>Python code:</p>
<pre class="lang-py prettyprint-override"><code>import os
import pandas as pd
from statsmodels.formula.api import ols
import statsmodels.api as sm
m = pd.read_csv(&quot;mut.csv&quot;, sep=&quot;,&quot;)
m=pd.DataFrame(m)
m.info()
g = pd.read_csv(&quot;expr1.csv&quot;, sep=&quot;,&quot;)
g=pd.DataFrame(g)

lst = list(g.columns)
m_lst = list(m.columns)

dfs = pd.DataFrame({})
for Mut in m_lst:
    for Model in lst:
        df = pd.concat([m[[Mut]].reset_index(drop=True), g[[Model]].reset_index(drop=True)],axis=1)
        df = df.rename(columns={Mut: &quot;mut&quot;, Model: &quot;g&quot;})
        model = ols('g ~ C(mut)', data=df).fit()
        aov_table = sm.stats.anova_lm(model, typ=2)
        aov_table[['Model']] = Model
        aov_table[['Mut']] = Mut
        dfs = dfs.append(aov_table)

with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    print(dfs)
            sum_sq   df         F    PR(&gt;F)     Model      Mut
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelM
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelM
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelM
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelM
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelO
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelO
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelO
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelO
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelS
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelS
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelS
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelS
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelA
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelA
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelA
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelA
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelI
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelI
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelI
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelI
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelC
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelC
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelC
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelC
C(mut)    0.061205  1.0  0.464539  0.514753  GeneA_KO   ModelT
Residual  1.054037  8.0       NaN       NaN  GeneA_KO   ModelT
C(mut)    0.063734  1.0  0.479234  0.508361  GeneB_KO   ModelT
Residual  1.063933  8.0       NaN       NaN  GeneB_KO   ModelT
C(mut)    0.245964  1.0  2.263619  0.170857  GeneA_KO   ModelH
Residual  0.869278  8.0       NaN       NaN  GeneA_KO   ModelH
C(mut)    0.312937  1.0  3.072788  0.117706  GeneB_KO   ModelH
Residual  0.814730  8.0       NaN       NaN  GeneB_KO   ModelH
C(mut)    0.001082  1.0  0.007771  0.931920  GeneA_KO   ModelE
Residual  1.114159  8.0       NaN       NaN  GeneA_KO   ModelE
C(mut)    0.220171  1.0  1.940909  0.201060  GeneB_KO   ModelE
Residual  0.907496  8.0       NaN       NaN  GeneB_KO   ModelE
C(mut)    0.052891  1.0  0.398296  0.545575  GeneA_KO   ModelR
Residual  1.062350  8.0       NaN       NaN  GeneA_KO   ModelR
C(mut)    0.090105  1.0  0.694741  0.428739  GeneB_KO   ModelR
Residual  1.037562  8.0       NaN       NaN  GeneB_KO   ModelR
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO  ModelA2
Residual  0.978877  8.0       NaN       NaN  GeneA_KO  ModelA2
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO  ModelA2
Residual  0.586718  8.0       NaN       NaN  GeneB_KO  ModelA2
C(mut)    0.004637  1.0  0.033399  0.859537  GeneA_KO   ModelP
Residual  1.110605  8.0       NaN       NaN  GeneA_KO   ModelP
C(mut)    0.276724  1.0  2.601573  0.145422  GeneB_KO   ModelP
Residual  0.850943  8.0       NaN       NaN  GeneB_KO   ModelP
C(mut)    0.044310  1.0  0.331000  0.580888  GeneA_KO  ModelE2
Residual  1.070932  8.0       NaN       NaN  GeneA_KO  ModelE2
C(mut)    0.170278  1.0  1.422852  0.267110  GeneB_KO  ModelE2
Residual  0.957389  8.0       NaN       NaN  GeneB_KO  ModelE2
C(mut)    0.001687  1.0  0.012120  0.915049  GeneA_KO   ModelU
Residual  1.113555  8.0       NaN       NaN  GeneA_KO   ModelU
C(mut)    0.008679  1.0  0.062048  0.809563  GeneB_KO   ModelU
Residual  1.118988  8.0       NaN       NaN  GeneB_KO   ModelU
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO  ModelT2
Residual  0.978877  8.0       NaN       NaN  GeneA_KO  ModelT2
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO  ModelT2
Residual  0.586718  8.0       NaN       NaN  GeneB_KO  ModelT2
C(mut)    0.017610  1.0  0.128353  0.729416  GeneA_KO  ModelI2
Residual  1.097631  8.0       NaN       NaN  GeneA_KO  ModelI2
C(mut)    0.260821  1.0  2.407079  0.159387  GeneB_KO  ModelI2
Residual  0.866846  8.0       NaN       NaN  GeneB_KO  ModelI2
C(mut)    0.191207  1.0  1.655405  0.234206  GeneA_KO  ModelC2
Residual  0.924035  8.0       NaN       NaN  GeneA_KO  ModelC2
C(mut)    0.033474  1.0  0.244741  0.634101  GeneB_KO  ModelC2
Residual  1.094193  8.0       NaN       NaN  GeneB_KO  ModelC2
C(mut)    0.044621  1.0  0.333425  0.579533  GeneA_KO  ModelS2
Residual  1.070620  8.0       NaN       NaN  GeneA_KO  ModelS2
C(mut)    0.096956  1.0  0.752533  0.410940  GeneB_KO  ModelS2
Residual  1.030711  8.0       NaN       NaN  GeneB_KO  ModelS2
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2023-09-21T20:17:00.760Z
Sorry I was running your python code but I get this error for Mut in m_lst:
  Input In [81]
    for Mut in m_lst:
                     ^
IndentationError: expected an indented block
Zizogolu
CC BY-SA 4.0
2023-09-22T00:04:50.037Z
The `IndentationError` means you haven't got the correct whitespace - you need 4 spaces in front of each indented line @Angel and no whitespace in front of 'non-indented' lines (basically, copy it exactly as I have it in my answer and it will work)
jared_mamrot
CC BY-SA 4.0
2023-09-22T09:26:15.927Z
Thanks a lot that's weird I just copied and pasted that but gives error
Zizogolu
CC BY-SA 4.0
2023-09-22T11:40:01.853Z
You're using spyder, so perhaps try going to "Source --> Fix Indentation" and see if your code changes and if the error persists
jared_mamrot
CC BY-SA 4.0
2023-09-22T13:49:22.213Z
Source fixed Indentation error now I get this Traceback (most recent call last):
  File "/Users/angel/Downloads/test/test.py", line 20, in <module>
    model = ols('g ~ C(mut)', data=df).fit()
  File "/Users/angel/Downloads/docker/.venv/lib/python3.9/site-packages/statsmodels/base/model.py", line 209, in from_formula
    raise ValueError('endog has evaluated to an array with multiple '
ValueError: endog has evaluated to an array with multiple columns that has shape (10, 18). This occurs when the variable converted to endog is non-numeric (e.g., bool or str).
Zizogolu
CC BY-SA 4.0
2023-09-22T21:58:32.203Z
That error is harder to understand, sorry @Angel; I think you should post another question with a reproducible example showing how you got that error so we can figure out what the problem is.
jared_mamrot
CC BY-SA 4.0
2023-09-23T05:42:37.007Z
Before using this-->  `with pd.option_context('display.max_rows', None, 'display.max_columns', None):`,  `print(dfs)` could display the 1st 10 rows,  tried, worked!
envs_h_gang_5
CC BY-SA 4.0
2023-09-23T05:53:17.210Z
Moreover, after using this--> `python -V`,  my z shell on my macbook air is using Python 3.9.16 .
envs_h_gang_5
CC BY-SA 4.0
2023-09-26T01:36:44.700Z
Apparently the `pd.option_context('display.max_rows', None, 'display.max_columns', None)` approach doesn't work with jupyter notebooks; see https://stackoverflow.com/questions/16424493/pandas-setting-no-of-max-rows for more details @envs_h_gang_5
jared_mamrot
CC BY-SA 4.0
2023-09-19T06:23:56.657Z
2023-09-24T15:27:20.803Z
2023-09-24T15:27:20.803Z
jared_mamrot
<p>I have two data sets</p>
<ol>
<li>Mutations in 10 genes</li>
<li>Gene expression in 18 cancer 3D models</li>
</ol>
<p><em>For every (mutation, gene) and gene expression pair we run an ANOVA test</em></p>
<p>This is my data</p>
<pre class="lang-r prettyprint-override"><code>
&gt; mut=read.delim(&quot;mut.tsv&quot;,row.names = 1)
&gt; expr=as.data.frame(t(read.delim(&quot;expr.tsv&quot;,row.names = 1)))

dput(expr)

structure(list(GeneA_KO = c(0.0756383119639074, -0.162841965682723, 
-0.110703100204517, -0.457154616239399, -0.505815193885352, 0.366072775033448, 
0.288650458935083, -0.0287646498640309, -0.690732824311905, 0.157366122620995, 
-1.29451546737684, 0.124167425460344, -0.168045313861751, 0.0186504943306465, 
-0.495880827400706, -0.206668627339351, -0.148609490335447, 0.066844529089804
), GeneB_KO = c(-0.870157285953621, -0.35900872537626, -0.990406888128838, 
-0.120656981579292, -1.41427631791049, -0.866421095836781, -0.816936112146099, 
-0.894597236248507, -1.00084584207823, -0.850754488133456, -1.05314239761653, 
-1.07580675428372, -1.24708324665688, -1.04098460460899, -0.232388675985086, 
-1.48513703581865, -1.48373348870346, -1.76484058374354), class = &quot;data.frame&quot;, row.names = c(&quot;ModelM&quot;, 
&quot;ModelO&quot;, &quot;ModelS&quot;, &quot;ModelA&quot;, &quot;ModelI&quot;, &quot;ModelC&quot;, &quot;ModelT&quot;, &quot;ModelH&quot;, 
&quot;ModelE&quot;, &quot;ModelR&quot;, &quot;ModelA2&quot;, &quot;ModelP&quot;, &quot;ModelE2&quot;, &quot;ModelU&quot;, 
&quot;ModelT2&quot;, &quot;ModelC2&quot;, &quot;ModelS2&quot;, &quot;ModelX&quot;))

dput(mut)

structure(list(ModelM = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L), ModelO = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelS = c(0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelA = c(0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L), ModelI = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 
0L, 0L, 0L), ModelC = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L
), ModelT = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L), ModelH = c(1L, 
1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L), ModelE = c(1L, 0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 1L), ModelR = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 
0L, 1L, 0L), ModelA2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L), ModelP = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L), ModelE2 = c(1L, 
0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L), ModelU = c(1L, 0L, 1L, 1L, 
0L, 1L, 0L, 1L, 1L, 1L), ModelT2 = c(0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L), ModelI2 = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L), ModelC2 = c(1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L), 
    ModelS2 = c(0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L)), class = &quot;data.frame&quot;, row.names = c(&quot;Gene1_mut&quot;, 
&quot;Gene2_mut&quot;, &quot;Gene3_mut&quot;, &quot;Gene4_mut&quot;, &quot;Gene5_mut&quot;, &quot;Gene6_mut&quot;, 
&quot;Gene7_mut&quot;, &quot;Gene8_mut&quot;, &quot;Gene9_mut&quot;, &quot;Gene10_mut&quot;))
</code></pre>
<p>To test this, for the first 3D cancer model as instance, I have done like below</p>
<pre><code>           GeneA_KO   GeneB_KO
ModelM   0.07563831 -0.8701573
ModelO  -0.16284197 -0.3590087
ModelS  -0.11070310 -0.9904069
ModelA  -0.45715462 -0.1206570
ModelI  -0.50581519 -1.4142763
ModelC   0.36607278 -0.8664211
ModelT   0.28865046 -0.8169361
ModelH  -0.02876465 -0.8945972
ModelE  -0.69073282 -1.0008458
ModelR   0.15736612 -0.8507545
ModelA2 -1.29451547 -1.0531424
ModelP   0.12416743 -1.0758068
ModelE2 -0.16804531 -1.2470832
ModelU   0.01865049 -1.0409846
ModelT2 -0.49588083 -0.2323887
ModelC2 -0.20666863 -1.4851370
ModelS2 -0.14860949 -1.4837335
ModelX   0.06684453 -1.7648406
</code></pre>
<pre><code>           ModelM ModelO ModelS ModelA ModelI ModelC ModelT ModelH ModelE
Gene1_mut       0      0      0      0      0      0      0      1      1
Gene2_mut       0      0      0      0      0      0      0      1      0
Gene3_mut       0      0      0      0      0      0      0      1      0
Gene4_mut       1      1      1      1      1      1      1      1      0
Gene5_mut       0      0      0      0      0      0      0      0      1
Gene6_mut       0      0      0      0      0      0      1      1      0
Gene7_mut       0      0      0      0      0      0      0      1      0
Gene8_mut       0      0      0      0      0      0      1      1      0
Gene9_mut       0      0      0      0      0      0      1      0      0
Gene10_mut      0      0      0      0      0      0      0      0      1
           ModelR ModelA2 ModelP ModelE2 ModelU ModelT2 ModelI2 ModelC2 ModelS2
Gene1_mut       1       0      0       1      1       0       1       1       0
Gene2_mut       0       0      0       0      0       0       0       1       1
Gene3_mut       0       0      0       0      1       0       0       0       0
Gene4_mut       1       1      1       1      1       1       1       0       1
Gene5_mut       0       0      0       0      0       0       0       0       1
Gene6_mut       0       0      0       1      1       0       0       1       1
Gene7_mut       1       0      0       0      0       0       0       0       1
Gene8_mut       0       0      0       0      1       0       0       1       0
Gene9_mut       1       0      0       0      1       0       0       0       0
Gene10_mut      0       0      1       0      1       0       0       0       1
</code></pre>
<pre><code>&gt; my_data=cbind(mut[,1],expr[,1])
&gt; colnames(my_data)=c(&quot;mut&quot;,&quot;expr&quot;)
&gt; res.aov &lt;- aov(expr ~ mut, data = as.data.frame(my_data))
&gt; # Summary of the analysis
&gt; summary(res.aov)
            Df Sum Sq Mean Sq F value Pr(&gt;F)
mut          1  0.145  0.1448   0.192  0.673
Residuals    8  6.022  0.7527
</code></pre>
<p>Now I do a for loop for all of the 18 cancer cell lines</p>
<pre><code>&gt; for(i in 1:ncol(g))
+ {
+   my_data=as.data.frame(cbind(m[,1],g[,1]))
+   #tidy will summarise and return neat format
+   print(broom::tidy(aov(my_data[,2] ~ my_data[,1], data = my_data)))
+ }
# A tibble: 2 × 6
  term            df sumsq meansq statistic p.value
  &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
1 my_data[, 1]     1 0.145  0.145     0.192   0.673
2 Residuals        8 6.02   0.753    NA      NA    
# A tibble: 2 × 6
  term            df sumsq meansq statistic p.value
  &lt;chr&gt;        &lt;dbl&gt; &lt;dbl&gt;  &lt;dbl&gt;     &lt;dbl&gt;   &lt;dbl&gt;
1 my_data[, 1]     1 0.145  0.145     0.192   0.673
2 Residuals        8 6.02   0.753    NA      NA   
</code></pre>
<p>I done this in R but of course I got when doing in Python</p>
<p><em>Python</em></p>
<pre class="lang-py prettyprint-override"><code>import os
import pandas as pd
from statsmodels.formula.api import ols
import statsmodels.api as sm
os.getcwd()
os.chdir(&quot;/Users//Downloads/test&quot;)
os.getcwd()
m = pd.read_csv(&quot;mut.tsv&quot;, sep=&quot;\t&quot;, index_col=0,delim_whitespace=True)
m=pd.DataFrame(m)
m
g = pd.read_csv(&quot;expr.tsv&quot;, sep=&quot;\t&quot;,index_col=0,delim_whitespace=True).T
g=pd.DataFrame(g)
g
lst = list(g.columns)

lst
dfs = []
for Model in lst:
   dfs.append(pd.concat([m.iloc[:, 0].reset_index(drop=True), g.iloc[0,:].reset_index(drop=True)],axis=1))
   dfs.append(df.dropna())
   dfs.append(df.columns==['mut','expr'])
   model = ols('expr ~ C(mut)', data=df).fit()
   aov_table = sm.stats.anova_lm(model, typ=2)
   aov_table
   final = pd.concat(dfs, ignore_index=True)
</code></pre>
<pre><code>    for Model in lst:
      Input In [165]
        for Model in lst:
                         ^
    IndentationError: expected an indented block
</code></pre>
<p><a href="https://i.sstatic.net/GYIIn.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GYIIn.jpg" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2023-09-18T12:00:57.700Z
When asking about errors, please remember to _always_ include the actual error. We cannot help you debug errors you don't share.
terdon
CC BY-SA 4.0
2023-09-18T13:33:52.993Z
thank you, I have added the error in my main post
Zizogolu
CC BY-SA 4.0
2023-09-18T13:51:34.513Z
@Angel, you still did not include the error that you get when you are using R. Anyway, why would you define `i` in your loop and not use it? And when it comes to the `Python` error, it is obvious!
haci
CC BY-SA 4.0
2023-09-18T13:54:45.763Z
Thank you @haci I get error in Python part only. The R code is OK
Zizogolu
CC BY-SA 4.0
2023-09-19T00:00:10.783Z
Thanks I have edited my post so I believe the R code will run smoothly. I have added the desire output from R code to the post. First, I have tried for one sample then using a for loop to doing one-way ANOVA over 18 samples. But, I do not know how to do this by Python
Zizogolu
<python>
Looping over a list of cell models in Python
# Looping over a list of cell models in Python

(Asked by: username_0 on 2023-09-16)

I have two data sets

Mutations in 10 genes
Gene expression in 18 cancer 3D models

For every (mutation, gene) and gene expression pair we run an ANOVA test
This is my data

> mut=read.delim("mut.tsv",row.names = 1)
> expr=as.data.frame(t(read.delim("expr.tsv",row.names = 1)))

dput(expr)

structure(list(GeneA_KO = c(0.0756383119639074, -0.162841965682723, 
-0.110703100204517, -0.457154616239399, -0.505815193885352, 0.366072775033448, 
0.288650458935083, -0.0287646498640309, -0.690732824311905, 0.157366122620995, 
-1.29451546737684, 0.124167425460344, -0.168045313861751, 0.0186504943306465, 
-0.495880827400706, -0.206668627339351, -0.148609490335447, 0.066844529089804
), GeneB_KO = c(-0.870157285953621, -0.35900872537626, -0.990406888128838, 
-0.120656981579292, -1.41427631791049, -0.866421095836781, -0.816936112146099, 
-0.894597236248507, -1.00084584207823, -0.850754488133456, -1.05314239761653, 
-1.07580675428372, -1.24708324665688, -1.04098460460899, -0.232388675985086, 
-1.48513703581865, -1.48373348870346, -1.76484058374354), class = "data.frame", row.names = c("ModelM", 
"ModelO", "ModelS", "ModelA", "ModelI", "ModelC", "ModelT", "ModelH", 
"ModelE", "ModelR", "ModelA2", "ModelP", "ModelE2", "ModelU", 
"ModelT2", "ModelC2", "ModelS2", "ModelX"))

dput(mut)

structure(list(ModelM = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L), ModelO = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelS = c(0L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelA = c(0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L, 0L), ModelI = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 
0L, 0L, 0L), ModelC = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L
), ModelT = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L), ModelH = c(1L, 
1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L), ModelE = c(1L, 0L, 0L, 0L, 
1L, 0L, 0L, 0L, 0L, 1L), ModelR = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 
0L, 1L, 0L), ModelA2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
0L), ModelP = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L), ModelE2 = c(1L, 
0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L), ModelU = c(1L, 0L, 1L, 1L, 
0L, 1L, 0L, 1L, 1L, 1L), ModelT2 = c(0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L, 0L, 0L), ModelI2 = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
0L, 0L), ModelC2 = c(1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L), 
    ModelS2 = c(0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L)), class = "data.frame", row.names = c("Gene1_mut", 
"Gene2_mut", "Gene3_mut", "Gene4_mut", "Gene5_mut", "Gene6_mut", 
"Gene7_mut", "Gene8_mut", "Gene9_mut", "Gene10_mut"))

To test this, for the first 3D cancer model as instance, I have done like below
           GeneA_KO   GeneB_KO
ModelM   0.07563831 -0.8701573
ModelO  -0.16284197 -0.3590087
ModelS  -0.11070310 -0.9904069
ModelA  -0.45715462 -0.1206570
ModelI  -0.50581519 -1.4142763
ModelC   0.36607278 -0.8664211
ModelT   0.28865046 -0.8169361
ModelH  -0.02876465 -0.8945972
ModelE  -0.69073282 -1.0008458
ModelR   0.15736612 -0.8507545
ModelA2 -1.29451547 -1.0531424
ModelP   0.12416743 -1.0758068
ModelE2 -0.16804531 -1.2470832
ModelU   0.01865049 -1.0409846
ModelT2 -0.49588083 -0.2323887
ModelC2 -0.20666863 -1.4851370
ModelS2 -0.14860949 -1.4837335
ModelX   0.06684453 -1.7648406

           ModelM ModelO ModelS ModelA ModelI ModelC ModelT ModelH ModelE
Gene1_mut       0      0      0      0      0      0      0      1      1
Gene2_mut       0      0      0      0      0      0      0      1      0
Gene3_mut       0      0      0      0      0      0      0      1      0
Gene4_mut       1      1      1      1      1      1      1      1      0
Gene5_mut       0      0      0      0      0      0      0      0      1
Gene6_mut       0      0      0      0      0      0      1      1      0
Gene7_mut       0      0      0      0      0      0      0      1      0
Gene8_mut       0      0      0      0      0      0      1      1      0
Gene9_mut       0      0      0      0      0      0      1      0      0
Gene10_mut      0      0      0      0      0      0      0      0      1
           ModelR ModelA2 ModelP ModelE2 ModelU ModelT2 ModelI2 ModelC2 ModelS2
Gene1_mut       1       0      0       1      1       0       1       1       0
Gene2_mut       0       0      0       0      0       0       0       1       1
Gene3_mut       0       0      0       0      1       0       0       0       0
Gene4_mut       1       1      1       1      1       1       1       0       1
Gene5_mut       0       0      0       0      0       0       0       0       1
Gene6_mut       0       0      0       1      1       0       0       1       1
Gene7_mut       1       0      0       0      0       0       0       0       1
Gene8_mut       0       0      0       0      1       0       0       1       0
Gene9_mut       1       0      0       0      1       0       0       0       0
Gene10_mut      0       0      1       0      1       0       0       0       1

> my_data=cbind(mut[,1],expr[,1])
> colnames(my_data)=c("mut","expr")
> res.aov <- aov(expr ~ mut, data = as.data.frame(my_data))
> # Summary of the analysis
> summary(res.aov)
            Df Sum Sq Mean Sq F value Pr(>F)
mut          1  0.145  0.1448   0.192  0.673
Residuals    8  6.022  0.7527

Now I do a for loop for all of the 18 cancer cell lines
> for(i in 1:ncol(g))
+ {
+   my_data=as.data.frame(cbind(m[,1],g[,1]))
+   #tidy will summarise and return neat format
+   print(broom::tidy(aov(my_data[,2] ~ my_data[,1], data = my_data)))
+ }
# A tibble: 2 × 6
  term            df sumsq meansq statistic p.value
                     
1 my_data[, 1]     1 0.145  0.145     0.192   0.673
2 Residuals        8 6.02   0.753    NA      NA    
# A tibble: 2 × 6
  term            df sumsq meansq statistic p.value
                     
1 my_data[, 1]     1 0.145  0.145     0.192   0.673
2 Residuals        8 6.02   0.753    NA      NA   

I done this in R but of course I got when doing in Python
Python
import os
import pandas as pd
from statsmodels.formula.api import ols
import statsmodels.api as sm
os.getcwd()
os.chdir("/Users//Downloads/test")
os.getcwd()
m = pd.read_csv("mut.tsv", sep="\t", index_col=0,delim_whitespace=True)
m=pd.DataFrame(m)
m
g = pd.read_csv("expr.tsv", sep="\t",index_col=0,delim_whitespace=True).T
g=pd.DataFrame(g)
g
lst = list(g.columns)

lst
dfs = []
for Model in lst:
   dfs.append(pd.concat([m.iloc[:, 0].reset_index(drop=True), g.iloc[0,:].reset_index(drop=True)],axis=1))
   dfs.append(df.dropna())
   dfs.append(df.columns==['mut','expr'])
   model = ols('expr ~ C(mut)', data=df).fit()
   aov_table = sm.stats.anova_lm(model, typ=2)
   aov_table
   final = pd.concat(dfs, ignore_index=True)

    for Model in lst:
      Input In [165]
        for Model in lst:
                         ^
    IndentationError: expected an indented block

**Tags:** python
> **Comments:**
> **username_1**: When asking about errors, please remember to _always_ include the actual error. We cannot help you debug errors you don't share.  
> **username_0**: thank you, I have added the error in my main post  
> **username_2**: @Angel, you still did not include the error that you get when you are using R. Anyway, why would you define `i` in your loop and not use it? And when it comes to the `Python` error, it is obvious!  
> **username_0**: Thank you @haci I get error in Python part only. The R code is OK  
> **username_0**: Thanks I have edited my post so I believe the R code will run smoothly. I have added the desire output from R code to the post. First, I have tried for one sample then using a for loop to doing one-way ANOVA over 18 samples. But, I do not know how to do this by Python  

---

## 1 answers

### Answer by username_3 on 2023-09-19. Score: 4 | Accepted answer

Here is one potential approach:
Example data:
expr1 <- structure(list(GeneA_KO = c(0.0756383119639074, -0.162841965682723, -0.110703100204517, -0.457154616239399, -0.505815193885352, 0.366072775033448, 0.288650458935083, -0.0287646498640309, -0.690732824311905, 0.157366122620995, -1.29451546737684, 0.124167425460344, -0.168045313861751, 0.0186504943306465, -0.495880827400706, -0.206668627339351, -0.148609490335447, 0.066844529089804), GeneB_KO = c(-0.870157285953621, -0.35900872537626, -0.990406888128838, -0.120656981579292, -1.41427631791049, -0.866421095836781, -0.816936112146099, -0.894597236248507, -1.00084584207823, -0.850754488133456, -1.05314239761653, -1.07580675428372, -1.24708324665688, -1.04098460460899, -0.232388675985086, -1.48513703581865, -1.48373348870346, -1.76484058374354)), class = "data.frame", row.names = c("ModelM", "ModelO", "ModelS", "ModelA", "ModelI", "ModelC", "ModelT", "ModelH", "ModelE", "ModelR", "ModelA2", "ModelP", "ModelE2", "ModelU", "ModelT2", "ModelC2", "ModelS2", "ModelX"))
mut <- structure(list(ModelM = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelO = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelS = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelA = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelI = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelC = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelT = c(0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L), ModelH = c(1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L), ModelE = c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L), ModelR = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L), ModelA2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelP = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L), ModelE2 = c(1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L), ModelU = c(1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L), ModelT2 = c(0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelI2 = c(1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), ModelC2 = c(1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L),     ModelS2 = c(0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L)), class = "data.frame", row.names = c("Gene1_mut", "Gene2_mut", "Gene3_mut", "Gene4_mut", "Gene5_mut", "Gene6_mut", "Gene7_mut", "Gene8_mut", "Gene9_mut", "Gene10_mut"))


R code:
my_data=cbind(mut[,1],expr1[1:10,1])
colnames(my_data)=c("mut","expr1")
res.aov <- aov(expr1 ~ mut, data = as.data.frame(my_data))
summary(res.aov)
#>             Df Sum Sq Mean Sq F value Pr(>F)
#> mut          1 0.1364  0.1364   1.114  0.322
#> Residuals    8 0.9789  0.1224

for (j in 1:nrow(mut)) {
  for(i in 1:ncol(expr1)) {
    my_data=as.data.frame(cbind(mut[,j],expr1[1:10,i]))
    print(broom::tidy(aov(my_data[,2] ~ my_data[,1], data = my_data)))
  }
}
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#> 2 Residuals        8 0.979  0.122     NA     NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#> 2 Residuals        8 0.587 0.0733     NA    NA     
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#> 2 Residuals        8 0.979  0.122     NA     NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#> 2 Residuals        8 0.587 0.0733     NA    NA     
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#> 2 Residuals        8 0.979  0.122     NA     NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#> 2 Residuals        8 0.587 0.0733     NA    NA     
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#> 2 Residuals        8 0.979  0.122     NA     NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#> 2 Residuals        8 0.587 0.0733     NA    NA     
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#> 2 Residuals        8 0.979  0.122     NA     NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#> 2 Residuals        8 0.587 0.0733     NA    NA     
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.136  0.136      1.11   0.322
#> 2 Residuals        8 0.979  0.122     NA     NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.541 0.541       7.38  0.0264
#> 2 Residuals        8 0.587 0.0733     NA    NA     
#> # A tibble: 2 × 6
#>   term            df  sumsq meansq statistic p.value
#>                       
#> 1 my_data[, 1]     1 0.0612 0.0612     0.465   0.515
#> 2 Residuals        8 1.05   0.132     NA      NA    
#> # A tibble: 2 × 6
#>   term            df  sumsq meansq statistic p.value
#>                       
#> 1 my_data[, 1]     1 0.0637 0.0637     0.479   0.508
#> 2 Residuals        8 1.06   0.133     NA      NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.246  0.246      2.26   0.171
#> 2 Residuals        8 0.869  0.109     NA     NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.313  0.313      3.07   0.118
#> 2 Residuals        8 0.815  0.102     NA     NA    
#> # A tibble: 2 × 6
#>   term            df   sumsq  meansq statistic p.value
#>                         
#> 1 my_data[, 1]     1 0.00108 0.00108   0.00777   0.932
#> 2 Residuals        8 1.11    0.139    NA        NA    
#> # A tibble: 2 × 6
#>   term            df sumsq meansq statistic p.value
#>                      
#> 1 my_data[, 1]     1 0.220  0.220      1.94   0.201
#> 2 Residuals        8 0.907  0.113     NA     NA    
#> # A tibble: 2 × 6
#>   term            df  sumsq meansq statistic p.value
#>                       
#> 1 my_data[, 1]     1 0.0529 0.0529     0.398   0.546
#> 2 Residuals        8 1.06   0.133     NA      NA    
#> # A tibble: 2 × 6
#>   term            df  sumsq meansq statistic p.value
#>                       
#> 1 my_data[, 1]     1 0.0901 0.0901     0.695   0.429
#> 2 Residuals        8 1.04   0.130     NA      NA

# write.csv(mut, "mut.csv", quote = FALSE, row.names = FALSE)
# write.csv(expr1[1:10,], "expr1.csv", quote = FALSE, row.names = FALSE)

Created on 2023-09-19 with reprex v2.0.2

Python code:
import os
import pandas as pd
from statsmodels.formula.api import ols
import statsmodels.api as sm
m = pd.read_csv("mut.csv", sep=",")
m=pd.DataFrame(m)
m.info()
g = pd.read_csv("expr1.csv", sep=",")
g=pd.DataFrame(g)

lst = list(g.columns)
m_lst = list(m.columns)

dfs = pd.DataFrame({})
for Mut in m_lst:
    for Model in lst:
        df = pd.concat([m[[Mut]].reset_index(drop=True), g[[Model]].reset_index(drop=True)],axis=1)
        df = df.rename(columns={Mut: "mut", Model: "g"})
        model = ols('g ~ C(mut)', data=df).fit()
        aov_table = sm.stats.anova_lm(model, typ=2)
        aov_table[['Model']] = Model
        aov_table[['Mut']] = Mut
        dfs = dfs.append(aov_table)

with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    print(dfs)
            sum_sq   df         F    PR(>F)     Model      Mut
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelM
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelM
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelM
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelM
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelO
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelO
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelO
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelO
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelS
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelS
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelS
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelS
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelA
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelA
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelA
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelA
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelI
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelI
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelI
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelI
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO   ModelC
Residual  0.978877  8.0       NaN       NaN  GeneA_KO   ModelC
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO   ModelC
Residual  0.586718  8.0       NaN       NaN  GeneB_KO   ModelC
C(mut)    0.061205  1.0  0.464539  0.514753  GeneA_KO   ModelT
Residual  1.054037  8.0       NaN       NaN  GeneA_KO   ModelT
C(mut)    0.063734  1.0  0.479234  0.508361  GeneB_KO   ModelT
Residual  1.063933  8.0       NaN       NaN  GeneB_KO   ModelT
C(mut)    0.245964  1.0  2.263619  0.170857  GeneA_KO   ModelH
Residual  0.869278  8.0       NaN       NaN  GeneA_KO   ModelH
C(mut)    0.312937  1.0  3.072788  0.117706  GeneB_KO   ModelH
Residual  0.814730  8.0       NaN       NaN  GeneB_KO   ModelH
C(mut)    0.001082  1.0  0.007771  0.931920  GeneA_KO   ModelE
Residual  1.114159  8.0       NaN       NaN  GeneA_KO   ModelE
C(mut)    0.220171  1.0  1.940909  0.201060  GeneB_KO   ModelE
Residual  0.907496  8.0       NaN       NaN  GeneB_KO   ModelE
C(mut)    0.052891  1.0  0.398296  0.545575  GeneA_KO   ModelR
Residual  1.062350  8.0       NaN       NaN  GeneA_KO   ModelR
C(mut)    0.090105  1.0  0.694741  0.428739  GeneB_KO   ModelR
Residual  1.037562  8.0       NaN       NaN  GeneB_KO   ModelR
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO  ModelA2
Residual  0.978877  8.0       NaN       NaN  GeneA_KO  ModelA2
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO  ModelA2
Residual  0.586718  8.0       NaN       NaN  GeneB_KO  ModelA2
C(mut)    0.004637  1.0  0.033399  0.859537  GeneA_KO   ModelP
Residual  1.110605  8.0       NaN       NaN  GeneA_KO   ModelP
C(mut)    0.276724  1.0  2.601573  0.145422  GeneB_KO   ModelP
Residual  0.850943  8.0       NaN       NaN  GeneB_KO   ModelP
C(mut)    0.044310  1.0  0.331000  0.580888  GeneA_KO  ModelE2
Residual  1.070932  8.0       NaN       NaN  GeneA_KO  ModelE2
C(mut)    0.170278  1.0  1.422852  0.267110  GeneB_KO  ModelE2
Residual  0.957389  8.0       NaN       NaN  GeneB_KO  ModelE2
C(mut)    0.001687  1.0  0.012120  0.915049  GeneA_KO   ModelU
Residual  1.113555  8.0       NaN       NaN  GeneA_KO   ModelU
C(mut)    0.008679  1.0  0.062048  0.809563  GeneB_KO   ModelU
Residual  1.118988  8.0       NaN       NaN  GeneB_KO   ModelU
C(mut)    0.136365  1.0  1.114460  0.321950  GeneA_KO  ModelT2
Residual  0.978877  8.0       NaN       NaN  GeneA_KO  ModelT2
C(mut)    0.540949  1.0  7.375925  0.026417  GeneB_KO  ModelT2
Residual  0.586718  8.0       NaN       NaN  GeneB_KO  ModelT2
C(mut)    0.017610  1.0  0.128353  0.729416  GeneA_KO  ModelI2
Residual  1.097631  8.0       NaN       NaN  GeneA_KO  ModelI2
C(mut)    0.260821  1.0  2.407079  0.159387  GeneB_KO  ModelI2
Residual  0.866846  8.0       NaN       NaN  GeneB_KO  ModelI2
C(mut)    0.191207  1.0  1.655405  0.234206  GeneA_KO  ModelC2
Residual  0.924035  8.0       NaN       NaN  GeneA_KO  ModelC2
C(mut)    0.033474  1.0  0.244741  0.634101  GeneB_KO  ModelC2
Residual  1.094193  8.0       NaN       NaN  GeneB_KO  ModelC2
C(mut)    0.044621  1.0  0.333425  0.579533  GeneA_KO  ModelS2
Residual  1.070620  8.0       NaN       NaN  GeneA_KO  ModelS2
C(mut)    0.096956  1.0  0.752533  0.410940  GeneB_KO  ModelS2
Residual  1.030711  8.0       NaN       NaN  GeneB_KO  ModelS2
```

> **Comments:**
> **username_0**: Sorry I was running your python code but I get this error for Mut in m_lst:   Input In [81]     for Mut in m_lst:                      ^ IndentationError: expected an indented block  
> **username_3**: The `IndentationError` means you haven't got the correct whitespace - you need 4 spaces in front of each indented line @Angel and no whitespace in front of 'non-indented' lines (basically, copy it exactly as I have it in my answer and it will work)  
> **username_0**: Thanks a lot that's weird I just copied and pasted that but gives error  
> **username_3**: You're using spyder, so perhaps try going to "Source --> Fix Indentation" and see if your code changes and if the error persists  
> **username_0**: Source fixed Indentation error now I get this Traceback (most recent call last):   File "/Users/angel/Downloads/test/test.py", line 20, in <module>     model = ols('g ~ C(mut)', data=df).fit()   File "/Users/angel/Downloads/docker/.venv/lib/python3.9/site-packages/statsmodels/base/model.py", line 209, in from_formula     raise ValueError('endog has evaluated to an array with multiple ' ValueError: endog has evaluated to an array with multiple columns that has shape (10, 18). This occurs when the variable converted to endog is non-numeric (e.g., bool or str).  
> **username_3**: That error is harder to understand, sorry @Angel; I think you should post another question with a reproducible example showing how you got that error so we can figure out what the problem is.  
> **username_4**: Before using this-->  `with pd.option_context('display.max_rows', None, 'display.max_columns', None):`,  `print(dfs)` could display the 1st 10 rows,  tried, worked!  
> **username_4**: Moreover, after using this--> `python -V`,  my z shell on my macbook air is using Python 3.9.16 .  
> **username_3**: Apparently the `pd.option_context('display.max_rows', None, 'display.max_columns', None)` approach doesn't work with jupyter notebooks; see https://stackoverflow.com/questions/16424493/pandas-setting-no-of-max-rows for more details @envs_h_gang_5  

---

