Python Forum

Full Version: How to modify df column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have data in excel and want to modify one column:

Name  Design   Category  Rank
DUT   UIK       Group2    5
HYT   TYAHU     Group1    1
PAT   JKAL      Group1    2
JKL   PLK       Group3    3
I want to modify column 2 (Design) from row 3 & add to each row ":POJK:WAT"


Desired output:

Name  Design      Category  Rank
DUT   UIK                 Group2    5
HYT   TYAHU:POJK:WAT      Group1    1
PAT   JKAL:POJK:WAT       Group1    2
JKL   PLK:POJK:WAT        Group3    3
I use below code:

df['Design']=df['Design'].astype(str)+[':POJK:WAT']
df['Design']=df['Design'].str.replace('UIK:POJK:WAT','UIK') % to replace in 2nd row ":POJK:WAT"
Since the third row in your example is the 2nd row of data and that Python indices are zero-based, you can slice the dataframe using [1:] as done here:
print(df)
df.loc[1:, 'Design'] = df.loc[1:, 'Design'] + ':POJK:WAT'
print(df)
Output:
Category Design Name Rank 0 Group2 UIK DUT 5 1 Group1 TYAHU HYT 1 2 Group1 JKAL PAT 2 3 Group3 PLK JKL 3 Category Design Name Rank 0 Group2 UIK DUT 5 1 Group1 TYAHU:POJK:WAT HYT 1 2 Group1 JKAL:POJK:WAT PAT 2 3 Group3 PLK:POJK:WAT JKL 3