如何利用Pandas制作直方图

2024-04-20 06:28:30 发布

您现在位置:Python中文网/ 问答频道 /正文

在这个问题中,使用pandas读取.txt文件。需要计算基因的数量,并为特定样本和每个基因的相互作用量制作直方图。你知道吗

我试过使用.transpose(),也试过使用value_counts()来访问适当的信息;但是,由于它位于一行中,并且表的设置方式不同,我无法找出如何获得适当的直方图。你知道吗

使用熊猫读取文件。编写一个程序来回答以下问题:

  1. 数据集中有多少个样本?你知道吗
  2. 数据集中有多少个基因?你知道吗
  3. 哪个样本的基因平均表达量最低?你知道吗
  4. 绘制显示IL6表达分布的直方图 所有样本。你知道吗

Data

 protein   M-12  M-24  M-36   M-48  M+ANDV-12  M+ANDV-24  M+ANDV-36  M+ANDV-48  M+SNV-12  M+SNV-24  M+SNV-36  M+SNV-48
    ARG1 -11.67 -9.92 -4.37 -11.92      -3.62      -9.38     -11.54      -4.88     -3.59     -2.96     -4.95     -4.31
   CASP3   0.05 -0.05 -0.18   0.02       0.04       0.14      -0.35      -0.41      0.24      0.23     -0.40     -0.36
   CASP7  -1.40 -0.05 -0.78  -1.33      -0.43       0.63      -1.39      -0.95      0.81      1.45      0.09      0.11
   CCL22  -0.96  1.47  0.37  -1.48       1.34       2.72     -11.12      -1.05     -0.63      1.42      0.30      0.12
    CCL5  -5.59 -3.84 -4.64  -5.84      -5.19      -5.24      -5.45      -5.45     -2.86     -4.53     -4.80     -6.46
    CCR7 -11.26 -9.50 -2.96 -11.50      -2.35      -2.31     -11.12      -3.66     -3.18     -1.31     -2.48     -2.84
    CD14   2.85  4.14  3.87   4.33       1.16       3.28       3.68       3.74      1.20      2.80      3.23      2.79
 CD200R1 -11.67 -9.92 -5.37 -11.92      -4.61      -9.38     -11.54     -11.54     -3.59     -2.96     -4.54     -4.89
   CD274  -5.59 -9.92 -4.64  -5.84      -1.78      -3.30      -5.45      -5.45     -4.17    -10.61     -4.80     -4.48
    CD80  -6.57 -9.50 -4.96  -6.82      -6.17      -4.28      -6.43      -6.43     -3.18     -5.51     -5.12     -4.16
    CD86   0.14  0.94  0.87   1.12      -0.23       0.58       1.09       0.66     -0.15      0.42      0.74      0.49
  CXCL10  -6.57 -2.85 -4.96  -6.82      -4.20      -2.31      -4.47      -4.47     -2.38     -2.74     -5.12     -4.67
  CXCL11  -5.28 -9.50 -5.63 -11.50     -10.85      -8.97     -11.12     -11.12     -9.83    -10.20     -5.79     -6.14
    IDO1  -5.02 -9.92 -4.37  -5.26      -4.61      -2.72      -4.88      -4.88     -2.60     -3.96     -4.54     -5.88
   IFNA1 -11.67 -9.92 -5.37  -5.26     -11.27      -9.38     -11.54      -4.88     -3.59    -10.61     -6.52     -5.88
   IFNB1 -11.67 -9.92 -6.35 -11.92     -11.27      -9.38     -11.54     -11.54    -10.25    -10.61    -12.19    -12.54
    IFNG  -2.09 -1.21 -1.66  -2.24      -2.75      -2.50      -2.83      -3.22     -2.48     -1.60     -2.13     -2.48
    IFR3  -0.39  0.05 -0.21   0.15      -0.27       0.07      -0.01      -0.11     -0.28      0.28      0.04     -0.09
    IL10  -1.53 -0.21 -0.51   0.45      -3.40      -1.00      -0.51      -0.04     -2.38     -1.55     -0.25     -0.72
   IL12A -11.67 -9.92 -4.79 -11.92      -3.30      -3.71     -11.54     -11.54    -10.25     -3.38     -4.22     -4.09
    IL15  -1.91 -2.53 -3.50  -3.85      -2.75      -9.38      -4.15      -4.15     -2.19     -2.09     -2.81     -3.16
    IL1A  -4.28 -2.53 -2.26  -3.39      -2.12      -0.51     -11.54      -2.67     -1.73     -1.75     -2.13     -1.84
    IL1B  -1.61 -2.53 -0.31  -0.16       0.77      -3.30      -1.95      -0.21     -1.73     -2.55     -0.65     -0.64
   IL1RN   3.14 -0.40 -1.54  -3.53       3.95       0.76       0.15      -3.15      3.34      0.95     -1.23     -1.02
     IL6  -4.60 -0.21 -1.82  -3.53      -1.25       0.76     -11.12      -2.47     -0.94     -0.60     -1.61     -1.74
     IL8   5.43  5.04  4.57   4.22       5.67       5.06       4.30       4.53      4.84      4.53      4.25      3.79
    IRF7   0.14  0.97 -0.13  -0.72       0.83       1.85      -0.19      -0.19      1.01      0.62      0.07     -0.03
   ITGAM  -1.68  0.91  0.28  -0.12       0.67       1.73      -0.30      -0.07      1.21      1.28      0.71      1.21
   NFKB1   0.80  0.31  0.29   0.43       1.21      -0.74       0.39       0.02      0.15     -0.02      0.01     -0.09
    NOS2 -11.26 -3.52 -4.50  -5.52      -4.87      -2.98      -5.14      -5.14     -3.85     -4.22     -5.79     -6.14
   PPARG   0.68  0.23  0.02  -1.16       0.56       1.38       0.80      -0.95      1.17      1.04      1.09      0.94
   TGFB1   3.99  3.21  2.41   2.62       4.05       3.48       2.87       2.15      3.68      2.97      2.46      2.31
    TLR3  -3.61 -1.85 -1.72 -11.92      -2.40      -1.32     -11.54     -11.54     -0.57      0.09     -1.32     -1.60
    TLR7  -3.80 -2.05 -1.64  -0.35      -6.17      -4.28      -2.47      -1.75     -3.18     -3.54     -1.86     -2.84
     TNF   1.09  0.53  0.71   1.17       1.91       0.58       1.04       1.41      1.20      1.18      1.13      0.66
   VEGFA  -2.36 -2.85 -3.64  -3.53      -3.40      -4.28      -4.47      -4.47     -5.15     -5.51     -4.32     -4.67
df=pd.read_csv('../Data/virus_miniset0.txt', sep='\t')

len(df['Sample'])

df

Tags: 文件数据txtpandasdfdata数量value
1条回答
网友
1楼 · 发布于 2024-04-20 06:28:30

设置索引,以便正确转置:

  • 在表格数据中,最上面一行应该指明每列的名称
    • 在这个数据中,第一个头被命名为sample,所有以M为前缀的名称都是样本。你知道吗
    • sample被重命名为protein,以正确标识列。你知道吗

当前数据:

import pandas as pd
import matplotlib.pylot as plt
import seaborn as sns

df.set_index('protein', inplace=True)

enter image description here

转置:

df_sample = df.T
df_sample.reset_index(inplace=True)
df_sample.rename(columns={'index': 'sample'}, inplace=True)
df_sample.set_index('sample', inplace=True)

enter image description here

样本数量:

len(df_sample.index)

>>> 12

有多少蛋白质/基因:

len(df_sample.columns)

>>> 36

最低平均表达量:

  • 找到mean,然后找到min
  • df_sample.mean().min()有效,但不包含蛋白质名称,只包含值。你知道吗
protein_avg = df_sample.mean()
protein_avg[protein_avg == df_sample.mean().min()]

>>> protein
    IFNB1   -10.765
    dtype: float64
  • 以下所有基因的箱线图确认IFNB1为样本中平均表达最低的蛋白质,并显示IL8为平均表达最高的蛋白质。你知道吗

箱线图:

  • seaborn使你的情节看起来更好
plt.figure(figsize=(12, 8))
g = sns.boxplot(data=df_sample)
for item in g.get_xticklabels():
    item.set_rotation(90)
plt.show()

enter image description here

备用箱线图:

plt.figure(figsize=(8, 8))
sns.boxplot('IL6', data=df_sample, orient='v')
plt.show()

enter image description here

IL6直方图:

sns.distplot(df_sample.IL6)
plt.show()

enter image description here

奖励情节-热图:

  • 我以为你会喜欢这个
plt.figure(figsize=(20, 8))
sns.heatmap(df_sample, annot=True, annot_kws={"size": 7}, cmap='PiYG')
plt.show()

enter image description here

  • M-12M+SNV-48在绘图中只有一半大小。这将在接下来的{}中解决

相关问题 更多 >