1

I have a data frame df like this:

        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". The experiment type is a list which I have created and it is: 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.

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?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, and what if my experiment types are not numbers? Let's say that I have different string names
@RGRGRG - It depends of length of DataFrame, not by column values. So it is no problem if strings, or if integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.