2021-12-10

Tech Programing

程式人小天地

Create a new column and assign values to the first row by each group in Python Pandas

1 min read


I have an existing Pandas Data Frame below:

   id  time  c  d
0   1     1  2  3
1   1     3  1  6
2   2     2  3  2
3   2     3  8  6

I also have values stored in a list such as:

list=[0.4, 0.6]

What I’m expecting to do is I want to create a new column in the existing DF and assign each list element in the first row for each group (id) such as:

   id  time  c  d  new_col
0   1     1  2  3  0.4
1   1     3  1  6
2   2     2  3  2  0.6
3   2     3  8  6



Source link

資料來源:Stackoverflow

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *