英文:
Python pandas script to automate transposing rows to column then getting the average of specific columns
问题
我有一个包含Elisa读数的CSV文件,其中每个凝集素都有三次不同浓度的实验数据,以及它们的光密度(OD)值。表格的行代表不同的凝集素,列代表不同的浓度。
示例表格:
NA | 浓度 1 | 浓度 2 |
---|---|---|
凝集素 1 | 1 | 2 |
凝集素 1 | 1.5 | 2 |
凝集素 1 | 2 | 2 |
凝集素 2 | 2 | 3 |
凝集素 2 | 2 | 3 |
凝集素 2 | 2 | 3 |
我想要将这个表格转置,使浓度成为行,凝集素成为列,然后计算每个凝集素不同浓度的平均值。总共有大约13个凝集素,每个凝集素有12个不同的浓度。能否提供用于实现这个目标的脚本?谢谢!
英文:
I have a csv file with a table of Elisa readings for lectins in triplicate with different concentration for each lectin and their OD values: so rows are the lectins and columns are the different concentration
Example table
NA | Concentration 1 | Concentration 2 |
---|---|---|
Lectin 1 | 1 | 2 |
Lectin 1 | 1.5 | 2 |
Lectin 1 | 2 | 2 |
Lectin 2 | 2 | 3 |
Lectin 2 | 2 | 3 |
Lectin 2 | 2 | 3 |
What I would like is to transpose this so that concentrations are rows and lectins are columns and then get the average values for of the different concentration from each lectin. there are about 13 lectins in triplicate with 12 different concentrations. Can someone advice the script for this? thanks!
答案1
得分: 1
你可以使用groupby
和mean()
来获得期望的结果,然后如果你想以那种方式表示数据,可以对数据框进行转置...
import pandas as pd
df_grouped = df.groupby("NA").mean().T
df_grouped
输出:
NA | Lectin 1 | Lectin 2 |
---|---|---|
Concentration 1 | 1.5 | 2 |
Concentration 2 | 2 | 3 |
英文:
You can groupby and apply mean() to get the expected result, then you can transpose the df if you want to express the data in that way...
import pandas as pd
df_grouped = df.groupby("NA").mean().T
df_grouped
Output:
NA | Lectin 1 | Lectin 2 |
---|---|---|
Concentration 1 | 1,5 | 2 |
Concentration 2 | 2 | 3 |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论