简体   繁体   中英

I am trying to combine multiple rows into single rows in python/pandas

I have three groups of rows right now and I am trying to combine those groups of rows into single rows. Current State of Data (Image only shows two genes, but I promise there's a third)

I have three separate genes, each having 61 NX values associated with them. I am trying to combine the 61 rows for each gene into a single row for each gene with all of its NX values listed across the row so that I can easily find the mean. I have no idea how to go about this. Please help.

Here is my code thus far if it matters.

 import pandas as pd

df = pd.read_csv('rna_consensus.tsv', delimiter='\t')

df = df[df['Gene name'].isin(["ENPP4", "RAD52", "ICA1"])]

df = df[['Gene name', 'NX']]

可以使用.groupby()GroupBy.mean()直接得到NX分组和Gene name的均值,如下:

df.groupby('Gene name', as_index=False)['NX'].mean()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM