One value for each df Column group

Tie_24
            A   B
0  2002-01-16  10
1  2002-01-16   7
2  2002-01-16   2
3  2002-01-16   8
4  2002-01-16   5
5  2002-01-17  54
6  2002-01-17   6
7  2002-01-17   2

I want to add a C column which contains the first Column B value for each Column A date group. The output might be:

            A   B   C
0  2002-01-16  10  10
1  2002-01-16   7  10
2  2002-01-16   2  10
3  2002-01-16   8  10
4  2002-01-16   5  10
5  2002-01-17  54  54
6  2002-01-17   6  54
7  2002-01-17   2  54

I´ve tested with:

df["C"] = df.values[0][1]

But it doesn´t change the value for each Column A date group.

Thank you.

Jon Clements

You can groupby column A, then use .transform('first') on column B to generate a series that has the first value of the group for all items in the group, eg:

df.loc[:, 'C'] = df.groupby('A').B.transform('first')

This'll make your example frame be:

            A   B   C
0  2002-01-16  10  10
1  2002-01-16   7  10
2  2002-01-16   2  10
3  2002-01-16   8  10
4  2002-01-16   5  10
5  2002-01-17  54  54
6  2002-01-17   6  54
7  2002-01-17   2  54

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

分類Dev

select a record from each group if it has given value in column otherwise any one record

分類Dev

Group rows where there is at least one column with true value

分類Dev

Grouping DataFrame, filtering by group size and by value of column in one line?

分類Dev

Retrieve just one value for each id in column A in excel

分類Dev

Group a dataframe on one column and take max from one column and its corresponding value from the other col

分類Dev

Value in each column of table

分類Dev

Group by column and select range of each group

分類Dev

R data.table: Rebase each group within the panel by a value found in another column

分類Dev

Value in one OR another column

分類Dev

Creating a new column and assigning values if any one of the row within a group contains a certain value

分類Dev

How to create partitioned table based on one Integer column (each value = 1 partition) in Oracle 12c?

分類Dev

GROUP BY Create group if at least one value in group meets condition

分類Dev

One column containing value and variable

分類Dev

How to select multiple columns and group by one column

分類Dev

DataFrame: Group by one column and average other columns

分類Dev

Split every row in df and add value to each element

分類Dev

MYSQL condense group by two columns into group by one column with breakdown

分類Dev

get the content of one column of each row in the table and then loop on each content

分類Dev

Clustering rows by group based on column value with conditions

分類Dev

Group Values in excel according duplicate value in column

分類Dev

mysql group records based on a column value

分類Dev

Preventing 1 level/value of pandas df column from being plotted

分類Dev

Calculate perc of each element in a list for each value in column in pandas dataframe

分類Dev

Retrieve last element of a column from each group and use it as first element of the same column in next group

分類Dev

How to change the column values of df1 with respect to single value in selected row of df2?

分類Dev

How to .map a column form 1 df for another df by checking the value in 2 columns

分類Dev

Append 2 Dataframes together only if a value from a column in df2 is in df1

分類Dev

Generate percentage for each group based on column values using Python pandas

Related 関連記事

  1. 1

    Use Spark to group by consecutive same values of one column, taking Max or Min value of another column for each group

  2. 2

    select a record from each group if it has given value in column otherwise any one record

  3. 3

    Group rows where there is at least one column with true value

  4. 4

    Grouping DataFrame, filtering by group size and by value of column in one line?

  5. 5

    Retrieve just one value for each id in column A in excel

  6. 6

    Group a dataframe on one column and take max from one column and its corresponding value from the other col

  7. 7

    Value in each column of table

  8. 8

    Group by column and select range of each group

  9. 9

    R data.table: Rebase each group within the panel by a value found in another column

  10. 10

    Value in one OR another column

  11. 11

    Creating a new column and assigning values if any one of the row within a group contains a certain value

  12. 12

    How to create partitioned table based on one Integer column (each value = 1 partition) in Oracle 12c?

  13. 13

    GROUP BY Create group if at least one value in group meets condition

  14. 14

    One column containing value and variable

  15. 15

    How to select multiple columns and group by one column

  16. 16

    DataFrame: Group by one column and average other columns

  17. 17

    Split every row in df and add value to each element

  18. 18

    MYSQL condense group by two columns into group by one column with breakdown

  19. 19

    get the content of one column of each row in the table and then loop on each content

  20. 20

    Clustering rows by group based on column value with conditions

  21. 21

    Group Values in excel according duplicate value in column

  22. 22

    mysql group records based on a column value

  23. 23

    Preventing 1 level/value of pandas df column from being plotted

  24. 24

    Calculate perc of each element in a list for each value in column in pandas dataframe

  25. 25

    Retrieve last element of a column from each group and use it as first element of the same column in next group

  26. 26

    How to change the column values of df1 with respect to single value in selected row of df2?

  27. 27

    How to .map a column form 1 df for another df by checking the value in 2 columns

  28. 28

    Append 2 Dataframes together only if a value from a column in df2 is in df1

  29. 29

    Generate percentage for each group based on column values using Python pandas

ホットタグ

アーカイブ