Replace a column in Pandas dataframe with another that has same index but in a different order

Dubraven

I'm trying to re-insert back into a pandas dataframe a column that I extracted and of which I changed the order by sorting it.

Very simply, I have extracted a column from a pandas df:

col1 = df.col1

This column contains integers and I used the .sort() method to order it from smallest to largest. And did some operation on the data.

col1.sort()
#do stuff that changes the values of col1.

Now the indexes of col1 are the same as the indexes of the overall df, but in a different order.

I was wondering how I can insert the column back into the original dataframe (replacing the col1 that is there at the moment)

I have tried both of the following methods:

1)

df.col1 = col1

2)

df.insert(column_index_of_col1, "col1", col1)

but both methods give me the following error:

ValueError: cannot reindex from a duplicate axis

Any help will be greatly appreciated. Thank you.

ayhan

Consider this DataFrame:

df = pd.DataFrame({'A': [1, 2, 3], 'B': [6, 5, 4]}, index=[0, 0, 1])

df
Out: 
   A  B
0  1  6
0  2  5
1  3  4

Assign the second column to b and sort it and take the square, for example:

b = df['B']
b = b.sort_values()
b = b**2

Now b is:

b
Out: 
1    16
0    25
0    36
Name: B, dtype: int64

Without knowing the exact operation you've done on the column, there is no way to know whether 25 corresponds to the first row in the original DataFrame or the second one. You can take the inverse of the operation (take the square root and match, for example) but that would be unnecessary I think. If you start with an index that has unique elements (df = df.reset_index()) it would be much easier. In that case,

df['B'] = b

should work just fine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sorting Pandas Dataframe by order of another index

From Dev

How to create another column that contains an operation based on two different values of a same categorical column in a pandas dataframe?

From Dev

Replace value in column with corresponding value from another column in same dataframe

From Dev

How to replace different strings with different values pretty in a column of dataframe by pandas?

From Dev

Efficiently replace values from a column to another column Pandas DataFrame

From Dev

Pandas lookup, mapping one column in a dataframe to another in a different dataframe

From Dev

Replace values in pandas dataframe column with different replacement dict based on condition

From Dev

Replace column values based on another dataframe python pandas - better way?

From Dev

Concatenate two pandas dataframe with the same index but on different positions

From Dev

Pandas Dataframe.Interpolate() gives different values for same index date

From Dev

how to combine two pandas dataframe with same format but different length index

From Dev

Pandas DataFrame - Combining one column's values with same index into list

From Dev

Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

From Dev

Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

From Dev

Different behavior of pandas when filter DataFrame by index and by column

From Dev

Pandas multiply several multi-index columns in a dataframe by another column

From Dev

Pandas how to copy a column to another dataframe with similar index

From Dev

Find row that has same value in one column over multiple rows while another column has different values

From Dev

Division in pandas: multiple columns by another column of the same DataFrame

From Dev

How to replace a dataframe column with another dataframe column

From Dev

update a column value in a dataframe from another matching column in different dataframe in Pandas

From Dev

Replace values in a pandas column using another pandas df which has the corresponding replacements

From Dev

How to replace values in a range in a pandas dataframe with another value in the same dataframe based on a condition

From Dev

Reshape dataframe to have the same index as another dataframe

From Java

Pandas replace values of a dataframe with the value of another dataframe

From Dev

How to plot different parts of same Pandas Series column with different colors, having a customized index?

From Dev

How to replace the column of dataframe based on priority order?

From Dev

New pandas DataFrame from another DataFrame based on a unique multiple column index

From Dev

Replace column in Pandas dataframe with the mean of that column

Related Related

  1. 1

    Sorting Pandas Dataframe by order of another index

  2. 2

    How to create another column that contains an operation based on two different values of a same categorical column in a pandas dataframe?

  3. 3

    Replace value in column with corresponding value from another column in same dataframe

  4. 4

    How to replace different strings with different values pretty in a column of dataframe by pandas?

  5. 5

    Efficiently replace values from a column to another column Pandas DataFrame

  6. 6

    Pandas lookup, mapping one column in a dataframe to another in a different dataframe

  7. 7

    Replace values in pandas dataframe column with different replacement dict based on condition

  8. 8

    Replace column values based on another dataframe python pandas - better way?

  9. 9

    Concatenate two pandas dataframe with the same index but on different positions

  10. 10

    Pandas Dataframe.Interpolate() gives different values for same index date

  11. 11

    how to combine two pandas dataframe with same format but different length index

  12. 12

    Pandas DataFrame - Combining one column's values with same index into list

  13. 13

    Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

  14. 14

    Aggregate by repeated datetime index with different identifiers in a column on a pandas dataframe

  15. 15

    Different behavior of pandas when filter DataFrame by index and by column

  16. 16

    Pandas multiply several multi-index columns in a dataframe by another column

  17. 17

    Pandas how to copy a column to another dataframe with similar index

  18. 18

    Find row that has same value in one column over multiple rows while another column has different values

  19. 19

    Division in pandas: multiple columns by another column of the same DataFrame

  20. 20

    How to replace a dataframe column with another dataframe column

  21. 21

    update a column value in a dataframe from another matching column in different dataframe in Pandas

  22. 22

    Replace values in a pandas column using another pandas df which has the corresponding replacements

  23. 23

    How to replace values in a range in a pandas dataframe with another value in the same dataframe based on a condition

  24. 24

    Reshape dataframe to have the same index as another dataframe

  25. 25

    Pandas replace values of a dataframe with the value of another dataframe

  26. 26

    How to plot different parts of same Pandas Series column with different colors, having a customized index?

  27. 27

    How to replace the column of dataframe based on priority order?

  28. 28

    New pandas DataFrame from another DataFrame based on a unique multiple column index

  29. 29

    Replace column in Pandas dataframe with the mean of that column

HotTag

Archive