<p>cluster_0 to cluster_12 are just strings. they cannot be the names of arrays or matrices. Strings can only be used in print statements and string processing statements.</p>

<p>The correct way to define an array or matrix is as follows: call the arrays as: cluster[0] to cluster[12]</p>

<p>then, assign:  </p>

<pre><code>  for i in range(12):
        cluster[i] = gene_list.gene[gene_list.cluster==i]
</code></pre>

<p>then whenever you need to print the names of these arrays, you use:  "cluster_"+str(i)</p>

CC BY-SA 4.0
2020-04-11T22:39:08.210Z
Okay .... yeah, this is better. There is an issue with the "cluster" dictionary (??), so I still think it will throw an error, but it is a definite improvement. The pandas command that is the formal solution is "iterrows" for this style of loop. – its a fun question.
M__
CC BY-SA 4.0
2020-04-11T20:52:56.490Z
2020-04-11T21:22:04.270Z
2020-04-11T21:22:04.270Z
A A Khodaparast
<p>This question comes up in my bioinfo class.</p>

<p>I have a csv file containing gene names and the cluster they belong to. I want to run GESA on each individual cluster. So I would like to extract the list of gene names corresponding to each cluster. So I wrote code like this</p>

<pre><code>import pandas as pd
gene_list = pd.read_csv('marker_genes.csv')
for i in range(12):
    "cluster_"+str(i) = gene_list.gene[gene_list.cluster==i]
</code></pre>

<p>I know this raises syntax error, so what is the correct way of doing it. 
In the end, I would want <code>cluster_0</code> to <code>cluster_12</code> each being a list of gene names.</p>

CC BY-SA 4.0
2020-04-11T08:46:46.350Z
You're looking for the `groupby()` function https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html
Devon Ryan
CC BY-SA 4.0
2020-04-11T13:01:52.307Z
Groupby is probably right, not always easy to get the object to spit out the values. Difficult without knowing the table format
M__
CC BY-SA 4.0
2020-04-11T13:12:49.637Z
Reshaping via melt is another possible way in or pivoting might work. There's probably lots of solutions. The pd.read_csv is a neat part of pandas, however you rarely use loops, only when you are stuck and its a different syntax.
M__
<python><pandas>
how do i change a variable with iterator under a for loop in python
# how do i change a variable with iterator under a for loop in python

(Asked by: username_0 on 2020-04-11)

This question comes up in my bioinfo class.
I have a csv file containing gene names and the cluster they belong to. I want to run GESA on each individual cluster. So I would like to extract the list of gene names corresponding to each cluster. So I wrote code like this
import pandas as pd
gene_list = pd.read_csv('marker_genes.csv')
for i in range(12):
    "cluster_"+str(i) = gene_list.gene[gene_list.cluster==i]

I know this raises syntax error, so what is the correct way of doing it. 
In the end, I would want cluster_0 to cluster_12 each being a list of gene names.

**Tags:** python, pandas
> **Comments:**
> **username_1**: You're looking for the `groupby()` function https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html  
> **username_2**: Groupby is probably right, not always easy to get the object to spit out the values. Difficult without knowing the table format  
> **username_2**: Reshaping via melt is another possible way in or pivoting might work. There's probably lots of solutions. The pd.read_csv is a neat part of pandas, however you rarely use loops, only when you are stuck and its a different syntax.  

---

## 1 answers

### Answer by username_3 on 2020-04-11. Score: 1

cluster_0 to cluster_12 are just strings. they cannot be the names of arrays or matrices. Strings can only be used in print statements and string processing statements.
The correct way to define an array or matrix is as follows: call the arrays as: cluster[0] to cluster[12]
then, assign:  
  for i in range(12):
        cluster[i] = gene_list.gene[gene_list.cluster==i]

then whenever you need to print the names of these arrays, you use:  "cluster_"+str(i)

> **Comments:**
> **username_2**: Okay .... yeah, this is better. There is an issue with the "cluster" dictionary (??), so I still think it will throw an error, but it is a definite improvement. The pandas command that is the formal solution is "iterrows" for this style of loop. – its a fun question.  

---

