How to create a data table in Python?

How to create a data table in Python?


When creating a data table in R, it seems simple.

genotype=rep(c("Genotype_A", "Genotype_B", "Genotype_C", "Genotype_D"), time=4)
block= rep(c("I", "II", "III", "IV"), time=16)
treatment= rep(c("Control", "Fertilizer1", "Fertilizer2", "Fertilizer3"), time=16)
yield= c(
    42.9, 41.6, 28.9, 30.8, 53.3, 69.6, 45.4, 35.1, 62.3, 58.5, 44.6, 50.3, 75.4,
    65.6, 54, 52.7, 53.8, 58.5, 43.9, 46.3, 57.6, 69.6, 42.4, 51.9, 63.4, 50.4,
    45, 46.7, 70.3, 67.3, 57.6, 58.5, 49.5, 53.8, 40.7, 39.4, 59.8, 65.8, 41.4,
    45.4, 64.5, 46.1, 62.6, 50.3, 68.8, 65.3, 45.6, 51, 44.4, 41.8, 28.3, 34.7,
    64.1, 57.4, 44.1, 51.6, 63.6, 56.1, 52.7, 51.8, 71.6, 69.4, 56.6, 47.4)

df= data.frame(genotype, block, treatment, yield)

df
     genotype block   treatment yield
1  Genotype_A     I     Control  42.9
2  Genotype_B    II Fertilizer1  41.6
3  Genotype_C   III Fertilizer2  28.9
4  Genotype_D    IV Fertilizer3  30.8
5  Genotype_A     I     Control  53.3
6  Genotype_B    II Fertilizer1  69.6
.
.
.

How about using Python?

import pandas as pd

genotypes = ["Genotype_A", "Genotype_B", "Genotype_C", "Genotype_D"] * 16
blocks = ["I", "II", "III", "IV"] * 16
variables = ["Control", "Fertilizer1", "Fertilizer2", "Fertilizer3"] * 16
values = [
        42.9, 41.6, 28.9, 30.8, 53.3, 69.6, 45.4, 35.1, 62.3, 58.5, 44.6, 
        50.3, 75.4, 65.6, 54, 52.7, 53.8, 58.5, 43.9, 46.3, 57.6, 69.6, 42.4, 
        51.9, 63.4, 50.4, 45, 46.7, 70.3, 67.3, 57.6, 58.5, 49.5, 53.8, 40.7, 
        39.4, 59.8, 65.8, 41.4, 45.4, 64.5, 46.1, 62.6, 50.3, 68.8, 65.3, 45.6, 
        51, 44.4, 41.8, 28.3, 34.7, 64.1, 57.4, 44.1, 51.6, 63.6, 56.1, 52.7, 
        51.8, 71.6, 69.4, 56.6, 47.4
        ]

df= pd.DataFrame({
    "Genotype": genotypes,
    "Block": blocks,
    "Variable": variables,
    "Value": values
})


Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.