Bottom Page

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"
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Add column to numpy matrix Gigux 1 123 Aug-02-2019, 12:05 AM
Last Post: scidam
  How to modify video using pixel array or shader? jhovarie 1 791 May-26-2019, 10:52 AM
Last Post: heiner55
  How to delete column if entire column values are "nan" Sri 4 428 Apr-13-2019, 12:16 PM
Last Post: Sri
  compare and modify columns in dataframe DionisiO 1 286 Feb-23-2019, 11:07 PM
Last Post: tiredAcademic
  How to modify and save a column in the sqlite3 database using python? laithsky1 3 463 Dec-18-2018, 03:39 AM
Last Post: micseydel
  Text to column pandas ms5573 0 493 Aug-25-2018, 08:18 PM
Last Post: ms5573
  Output column names Scott 2 668 Jun-25-2018, 12:47 PM
Last Post: volcano63
  how often does a word occur in this column? Jack_Sparrow 6 1,090 Jun-10-2018, 08:52 PM
Last Post: volcano63
  Add column headers to dataframe chisox721 5 2,195 May-20-2018, 12:31 AM
Last Post: volcano63
  How to find column index and its corresponding column name Raj 2 680 May-10-2018, 12:30 PM
Last Post: volcano63

Forum Jump:


Users browsing this thread: 1 Guest(s)