Python pandas脚本用于自动将行转置为列,然后获取特定列的平均值。

huangapple go评论62阅读模式
英文:

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

你可以使用groupbymean()来获得期望的结果,然后如果你想以那种方式表示数据,可以对数据框进行转置...

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

huangapple
  • 本文由 发表于 2023年2月16日 08:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定