你能告诉我 Python 中的 rep() 函数吗?
Could you let me know about rep() function in Python?
就像下面的 R 代码,我怎样才能简单地在 Python 中生成相同的数据 table?
Genotype <- c(rep(c("CV1","CV2", "CV3"), each=9))
Treatment <- c(rep(c("TR1", "TR2", "TR3"), each=3),
rep(c("TR1", "TR2", "TR3"), each=3),
rep(c("TR1", "TR2", "TR3"), each=3))
Block <- c(rep(c("B1","B2","B3"), times=9))
yield <- c(rep("15",5), rep("18",5), rep("20",8), rep("14",7), rep ("21",2))
dataA<- data.frame (Genotype, Treatment, Block, yield)
dataA
这是我生成的 Python 代码,但我相信更简单的方法。你能告诉我如何使用 R 中的 rep()
编写简单的代码吗?
import pandas
from pandas import DataFrame
source={'Genotype':["CV1","CV1","CV1","CV1","CV1","CV1","CV1","CV1","CV1","CV2","CV2",
"CV2","CV2","CV2","CV2","CV2","CV2","CV2","CV3","CV3","CV3","CV3",
"CV3","CV3", "CV3","CV3","CV3"],
'Treatment':["TR1","TR1","TR1","TR2","TR2","TR2","TR3","TR3","TR3","TR1","TR1","TR1","TR2","TR2",
"TR2","TR3","TR3","TR3","TR1","TR1","TR1","TR2","TR2","TR2","TR3","TR3","TR3"],
'Block':["B1","B2","B3","B1","B2","B3","B1","B2","B3","B1","B2","B3","B1","B2","B3",
"B1","B2","B3","B1","B2","B3","B1","B2","B3","B1","B2","B3"],
'Yield':[15,15,15,15,15,18,18,18,18,18,20,20,20,20,20,20,20,20,14,14,14,14,14,14,14,21,21]}
DataA=DataFrame(source)
DataA
永远感谢!!
df = pd.DataFrame(
{
"Genotype": ["CV1"] * 9 + ["CV2"] * 9 + ["CV3"] * 9,
"Treatment": (["TR1"] * 3 + ["TR2"] * 3 + ["TR3"] * 3) * 3,
"Block": ["B1", "B2", "B3"] * 9,
"yield": [15] * 5 + [18] * 5 + [20] * 8 + [14] * 7 + [21] * 2,
}
)
print(df)
打印:
Genotype Treatment Block yield
0 CV1 TR1 B1 15
1 CV1 TR1 B2 15
2 CV1 TR1 B3 15
3 CV1 TR2 B1 15
4 CV1 TR2 B2 15
5 CV1 TR2 B3 18
6 CV1 TR3 B1 18
7 CV1 TR3 B2 18
8 CV1 TR3 B3 18
9 CV2 TR1 B1 18
10 CV2 TR1 B2 20
11 CV2 TR1 B3 20
12 CV2 TR2 B1 20
13 CV2 TR2 B2 20
14 CV2 TR2 B3 20
15 CV2 TR3 B1 20
16 CV2 TR3 B2 20
17 CV2 TR3 B3 20
18 CV3 TR1 B1 14
19 CV3 TR1 B2 14
20 CV3 TR1 B3 14
21 CV3 TR2 B1 14
22 CV3 TR2 B2 14
23 CV3 TR2 B3 14
24 CV3 TR3 B1 14
25 CV3 TR3 B2 21
26 CV3 TR3 B3 21
感谢大家的回答。我解决了并分享了代码。我刚开始学习 Python,我的编码很基础。
import pandas as pd
from pandas import DataFrame
source = {"Genotype": ["CV1"]*9 + ["CV2"]*9 + ["CV3"]*9,
"Treatment": (["TR1"]*3 + ["TR2"]*3 + ["TR3"]*3)*3,
"Block": ["B1","B2","B3"]*9,
"yield": [15]* 5 + [18]* 5 + [20]* 8 + [14]* 7 + [21]* 2}
DataA=DataFrame(source)
DataA
就像下面的 R 代码,我怎样才能简单地在 Python 中生成相同的数据 table?
Genotype <- c(rep(c("CV1","CV2", "CV3"), each=9))
Treatment <- c(rep(c("TR1", "TR2", "TR3"), each=3),
rep(c("TR1", "TR2", "TR3"), each=3),
rep(c("TR1", "TR2", "TR3"), each=3))
Block <- c(rep(c("B1","B2","B3"), times=9))
yield <- c(rep("15",5), rep("18",5), rep("20",8), rep("14",7), rep ("21",2))
dataA<- data.frame (Genotype, Treatment, Block, yield)
dataA
这是我生成的 Python 代码,但我相信更简单的方法。你能告诉我如何使用 R 中的 rep()
编写简单的代码吗?
import pandas
from pandas import DataFrame
source={'Genotype':["CV1","CV1","CV1","CV1","CV1","CV1","CV1","CV1","CV1","CV2","CV2",
"CV2","CV2","CV2","CV2","CV2","CV2","CV2","CV3","CV3","CV3","CV3",
"CV3","CV3", "CV3","CV3","CV3"],
'Treatment':["TR1","TR1","TR1","TR2","TR2","TR2","TR3","TR3","TR3","TR1","TR1","TR1","TR2","TR2",
"TR2","TR3","TR3","TR3","TR1","TR1","TR1","TR2","TR2","TR2","TR3","TR3","TR3"],
'Block':["B1","B2","B3","B1","B2","B3","B1","B2","B3","B1","B2","B3","B1","B2","B3",
"B1","B2","B3","B1","B2","B3","B1","B2","B3","B1","B2","B3"],
'Yield':[15,15,15,15,15,18,18,18,18,18,20,20,20,20,20,20,20,20,14,14,14,14,14,14,14,21,21]}
DataA=DataFrame(source)
DataA
永远感谢!!
df = pd.DataFrame(
{
"Genotype": ["CV1"] * 9 + ["CV2"] * 9 + ["CV3"] * 9,
"Treatment": (["TR1"] * 3 + ["TR2"] * 3 + ["TR3"] * 3) * 3,
"Block": ["B1", "B2", "B3"] * 9,
"yield": [15] * 5 + [18] * 5 + [20] * 8 + [14] * 7 + [21] * 2,
}
)
print(df)
打印:
Genotype Treatment Block yield
0 CV1 TR1 B1 15
1 CV1 TR1 B2 15
2 CV1 TR1 B3 15
3 CV1 TR2 B1 15
4 CV1 TR2 B2 15
5 CV1 TR2 B3 18
6 CV1 TR3 B1 18
7 CV1 TR3 B2 18
8 CV1 TR3 B3 18
9 CV2 TR1 B1 18
10 CV2 TR1 B2 20
11 CV2 TR1 B3 20
12 CV2 TR2 B1 20
13 CV2 TR2 B2 20
14 CV2 TR2 B3 20
15 CV2 TR3 B1 20
16 CV2 TR3 B2 20
17 CV2 TR3 B3 20
18 CV3 TR1 B1 14
19 CV3 TR1 B2 14
20 CV3 TR1 B3 14
21 CV3 TR2 B1 14
22 CV3 TR2 B2 14
23 CV3 TR2 B3 14
24 CV3 TR3 B1 14
25 CV3 TR3 B2 21
26 CV3 TR3 B3 21
感谢大家的回答。我解决了并分享了代码。我刚开始学习 Python,我的编码很基础。
import pandas as pd
from pandas import DataFrame
source = {"Genotype": ["CV1"]*9 + ["CV2"]*9 + ["CV3"]*9,
"Treatment": (["TR1"]*3 + ["TR2"]*3 + ["TR3"]*3)*3,
"Block": ["B1","B2","B3"]*9,
"yield": [15]* 5 + [18]* 5 + [20]* 8 + [14]* 7 + [21]* 2}
DataA=DataFrame(source)
DataA