Python Forum
How to modify df column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to modify df column
#1
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"
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to modify and save a column in the sqlite3 database using python? laithsky1 3 2,762 Dec-18-2018, 03:39 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

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