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
  Camelot not able to change column header shortmeister1 2 820 Mar-22-2025, 04:47 AM
Last Post: Pedroski55
  Transform 3 Columns into Single Column DaveG 9 3,620 Mar-19-2025, 03:46 AM
Last Post: robbert23
  Replace values in Yaml file with value in dictionary PelleH 1 2,258 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  renaming a column without a name in a dataframe Carbonpony 2 1,118 Jan-23-2025, 08:20 AM
Last Post: Carbonpony
  Updating column name with translation bobbydave 0 1,228 Sep-17-2024, 03:40 PM
Last Post: bobbydave
  Assigning cycle values in a list nmancini 3 1,035 Sep-16-2024, 09:35 PM
Last Post: deanhystad
  Append from csv to xlsx with values only Sick_Stigma 2 781 Aug-06-2024, 08:05 PM
Last Post: Sick_Stigma
  adding a calculated column charles986 1 937 Jun-13-2024, 05:31 PM
Last Post: deanhystad
  adding a calculated column charles986 1 732 Jun-13-2024, 02:27 PM
Last Post: deanhystad
  remove duplicates from dicts with list values wardancer84 27 6,082 May-27-2024, 04:54 PM
Last Post: wardancer84

Forum Jump:

User Panel Messages

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