Python Forum
Copying column values up based on other column values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copying column values up based on other column values
#1
Hello,

I have this dataframe

import numpy as np
import pandas as pd
from numpy.random import randn
df=pd.DataFrame(randn(5,4),['A','B','C','D','E'],['W','X','Y','Z'])
Output:
W X Y Z A -0.183141 -0.398652 0.909746 0.332105 B -0.587611 -2.046930 1.446886 0.167606 C 1.142661 -0.861617 -0.180631 1.650463 D 1.174805 -0.957653 1.854577 0.335818 E -0.680611 -1.051793 1.448004 -0.490869
is there a way to create a column S - which will copy column column Y values UP- if values in column Y are above 1 - otherwise return new value above zero?.I made this manually:

Output:
S: A 1.446886 B 1.446886 C 1.854577 D 1.854577 E 1.448004
thanks a lot!
Larz60+ write Jan-03-2021, 04:42 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use code tags on future posts.
Reply
#2
thank you - I included all the code...the last column S was manually created by me as a reference for the code required for this operation...in excel there is a quick way to back-propagate a cell value by making it equal to the next one - eg A10=A11 - is there a quick way to do that in pandas given that it operates on arrays?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 245 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 222 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Too much values to unpack actualpy 3 409 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Help copying a column from a csv to another file with some extras g0nz0uk 3 403 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  Converting column of values into muliple columns of counts highland44 0 205 Feb-01-2024, 12:48 AM
Last Post: highland44
  Python code to set column width 1418 11 943 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  __init__() got multiple values for argument 'schema' dawid294 4 1,888 Jan-03-2024, 09:42 AM
Last Post: buran
  How to access values returned from inquirer cspower 6 700 Dec-26-2023, 09:34 PM
Last Post: cspower
  partial functions before knowing the values mikisDeWitte 4 536 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020