How to convert a triplet DataFrame to a new DataFrame with no duplicate rows?

o0Helloworld0o

I have two pandas DataFrames named df1 and df2, which are defined as follow:

>> df1 = pd.DataFrame( {
'id':       ['A', 'A', 'A', 'B', 'B', 'C', 'C'],
'year':     [2015, 2016, 2017, 2016, 2017, 2015, 2017],
'amount':   [1, 2, 3, 4, 5, 6, 7] } )

>> df2 = pd.DataFrame( {
    'id':   ['A', 'B', 'C'],
    '2015': [1, np.NaN, 6],
    '2016': [2, 4, np.NaN],
    '2017': [3, 5, 7] } )

>> df1
   amount id  year
0       1  A  2015
1       2  A  2016
2       3  A  2017
3       4  B  2016
4       5  B  2017
5       6  C  2015
6       7  C  2017

>> df2
   2015  2016  2017 id
0   1.0   2.0     3  A
1   NaN   4.0     5  B
2   6.0   NaN     7  C

As you can see, these two DataFrames contain the same information. DataFrame df1 is a triplet while df2 is a DataFrame with no duplicate values in its field id.

My question is how can I use pandas to convert df1 to df2 in a effective way ( without a for loop ) ? And how about df2 to df1?

Vaishali

You can pivot df1 to get df2 like this:

pd.pivot_table(df1, index='id', columns='year', values = 'amount')


year    2015    2016    2017
id          
A       1       2       3
B       NaN     4       5
C       6       NaN     7

And melt to do the reverse

pd.melt(df2, id_vars=["id"],var_name="year", value_name="amount").dropna()


    id  year    amount
0   A   2015    1
2   C   2015    6
3   A   2016    2
4   B   2016    4
6   A   2017    3
7   B   2017    5
8   C   2017    7

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R: how to join the duplicate rows in one dataframe

From Dev

How to count duplicate rows in pandas dataframe?

From Dev

How to efficiently remove duplicate rows from a DataFrame

From Java

How to convert a dict to a dataframe, with the values as headers and rows?

From Dev

How to convert rows in DataFrame in Python to dictionaries

From Java

How to convert DataFrame column to Rows in Python?

From Java

How to filter rows with specified conditions in a dataframe and put them in a new dataframe?

From Dev

pandas DataFrame sort rows by duplicate

From Dev

Consolidating duplicate rows in a large dataframe

From Dev

convert dataframe to new list

From Dev

how could i delete rows with repeating/duplicate index from dataframe

From Dev

pandas dataframe convert columns as rows

From Dev

Convert dataframe of list in columns to rows

From Java

all dataframe rows to new columns

From Dev

Insert new rows in pandas dataframe

From Dev

Add new rows to pyspark Dataframe

From Dev

Pandas new dataframe by rolling the rows

From Dev

How to create a new Pandas DataFrame from alternating boolean rows such that the new DataFrame is ready to plot?

From Dev

Make a duplicate value as name of a column in new dataframe with rows as the corresponding values in an other column

From Dev

How to convert column values to rows for each unique value in a dataframe in R?

From Dev

How to convert python JSON rows to dataframe columns without looping

From Dev

duplicate rows of dataframe by factor level index

From Dev

removing duplicate rows in pandas DataFrame based on a condition

From Java

Remove rows with duplicate indices (Pandas DataFrame and TimeSeries)

From Dev

Spark remove duplicate rows from DataFrame

From Dev

Add duplicate dataframe rows in a call to apply()

From Dev

Removing Duplicate rows and Calculate the Average in a Dataframe in R

From Dev

Pandas dataframe perform calculations on duplicate rows

From Dev

add duplicate rows to R dataframe based on sequence

Related Related

  1. 1

    R: how to join the duplicate rows in one dataframe

  2. 2

    How to count duplicate rows in pandas dataframe?

  3. 3

    How to efficiently remove duplicate rows from a DataFrame

  4. 4

    How to convert a dict to a dataframe, with the values as headers and rows?

  5. 5

    How to convert rows in DataFrame in Python to dictionaries

  6. 6

    How to convert DataFrame column to Rows in Python?

  7. 7

    How to filter rows with specified conditions in a dataframe and put them in a new dataframe?

  8. 8

    pandas DataFrame sort rows by duplicate

  9. 9

    Consolidating duplicate rows in a large dataframe

  10. 10

    convert dataframe to new list

  11. 11

    how could i delete rows with repeating/duplicate index from dataframe

  12. 12

    pandas dataframe convert columns as rows

  13. 13

    Convert dataframe of list in columns to rows

  14. 14

    all dataframe rows to new columns

  15. 15

    Insert new rows in pandas dataframe

  16. 16

    Add new rows to pyspark Dataframe

  17. 17

    Pandas new dataframe by rolling the rows

  18. 18

    How to create a new Pandas DataFrame from alternating boolean rows such that the new DataFrame is ready to plot?

  19. 19

    Make a duplicate value as name of a column in new dataframe with rows as the corresponding values in an other column

  20. 20

    How to convert column values to rows for each unique value in a dataframe in R?

  21. 21

    How to convert python JSON rows to dataframe columns without looping

  22. 22

    duplicate rows of dataframe by factor level index

  23. 23

    removing duplicate rows in pandas DataFrame based on a condition

  24. 24

    Remove rows with duplicate indices (Pandas DataFrame and TimeSeries)

  25. 25

    Spark remove duplicate rows from DataFrame

  26. 26

    Add duplicate dataframe rows in a call to apply()

  27. 27

    Removing Duplicate rows and Calculate the Average in a Dataframe in R

  28. 28

    Pandas dataframe perform calculations on duplicate rows

  29. 29

    add duplicate rows to R dataframe based on sequence

HotTag

Archive