[英]Create column with multiple names in python data frame
I have a data frame df like this: 我有一个像这样的数据框df:
Country Value
US 300
FR 360
IT 500
NL 450
CH 700
CN 233
NF 123
AR 456
BR 129
NG 423
.. ..
.. ..
GR 560
CY 145
ES 890
PR 783
BL 123
and I know that these countries in the df are the top 5 countries for each "experiment type". 我知道df中的这些国家是每种“实验类型”的前5个国家。 The experiment type is a list which I have created and it is:
my_list = [1, 2, 3, 4, 5, ......,20 ]
. 实验类型是我创建的列表,它是:
my_list = [1, 2, 3, 4, 5, ......,20 ]
。 Therefore, the first 5 countries belong to experiment 1, the next 5 countries belong to experiment 2,... .....and the last 5 countries belong to experiment 20. 因此,前5个国家属于实验1,接下来的5个国家属于实验2,……...,后5个国家属于实验20。
I want to create a column which is going to indicate the countries by experiment, so I want this: 我想创建一列,该列将通过实验指示国家/地区,因此我想要这样做:
Experiment Country Value
1 US 300
FR 360
IT 500
NL 450
CH 700
2 CN 233
NF 123
AR 456
BR 129
NG 423
.. ..
.. ..
20 GR 560
CY 145
ES 890
PR 783
BL 123
I know that something like this could be done while creating the data frame using groupby
function but could you help on which is the best way to do this now, after the initial data frame has been created? 我知道在使用
groupby
函数创建数据框时可以执行类似的操作,但是在创建初始数据框后,您能帮上什么是最好的方法吗?
I believe the best way to do this will be to add a new column with the experiment value, like this: 我相信最好的方法是使用实验值添加一个新列,如下所示:
df['Experiment'] = np.arange(len(df)) // 5
print (df)
Country Value Experiment
0 US 300 0
1 FR 360 0
2 IT 500 0
3 NL 450 0
4 CH 700 0
5 CN 233 1
6 NF 123 1
7 AR 456 1
8 BR 129 1
9 NG 423 1
10 GR 560 2
11 CY 145 2
12 ES 890 2
13 PR 783 2
14 BL 123 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.