Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How modify the DataFrame columns
I have below DataFrame,
I want to add in "0th" row if the columns are STD, the modify the "0th" row as the:
original cell value +"STD:JPEG:"
and if the column is "Peak" then modify the cell value in "0th" row as:
original cell value +"Peak:JPEG:"
I use the below code, but it is adding multiple time. For example, STD is in three columns, the cell value is modifying as:
"CplSTD:JPEG:STD:JPEG:STD:JPEG:" but it should be: CplSTD:JPEG

My input data:
Name	Category	STD			        Peak	
Name	Category	Cpl	BNP	Regulator	Power	Voltage
HAJ	RT	44	6	12	45	34
LKO	SP	4	34	33	13	122
STD is merged for three columns, and Peak is merged for two columns.

import pandas as pd

df.rename( columns={'Unnamed: 3':'STD'}, inplace=True )
df.rename( columns={'Unnamed: 4':'STD'}, inplace=True )
df.rename( columns={'Unnamed: 3':'STD'}, inplace=True )
df.rename( columns={'Unnamed: 6':'Peak'}, inplace=True )

for i in range(len(list(df.columns))):
    if df.columns[i] == 'Name' or df.columns[i] == 'Category':
    elif(df.columns[i] == 'STD'):
        df.loc[0,df.columns[i]] = df.loc[0,df.columns[i]]+"STD:JPEG:"
    elif(df.columns[i] == 'Peak'):
        df.loc[0,df.columns[i]] = df.loc[0,df.columns[i]]+"Peak:JPEG:"

Not entirely sure what you're trying to do here, but something like this should work:

import pandas as pd
df = pd.DataFrame(columns=['Name','Category','STD'])
df = df.append({'Name': 'HAJ', 'Category': 'RT', 'STD': 'Cpl'}, ignore_index=True)
df.iloc[0]['STD'] = df.iloc[0]['STD'] + 'STD:JPEG'
If the column is STD, then I want to add in each cell (columns belongs to STD) of 0th row "The cell value + STD +JPEG"
If the column is Peak, then add to each cell (columns belongs to Peak) in 0th row "The cell value +Peak+JPEG"

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Double 'for' loop and writing in a new columns dataframe marco_ita 0 91 Sep-07-2019, 12:44 PM
Last Post: marco_ita
  Creating A List of DataFrames & Manipulating Columns in Each DataFrame firebird 1 151 Jul-31-2019, 04:04 AM
Last Post: scidam
  [pandas] How to re-arrange DataFrame columns SriMekala 8 782 Jun-22-2019, 12:55 AM
Last Post: scidam
  Selecting Few Columns from a dataframe Shivi_Bhatia 2 378 Mar-24-2019, 12:20 PM
Last Post: Shivi_Bhatia
  compare and modify columns in dataframe DionisiO 1 322 Feb-23-2019, 11:07 PM
Last Post: tiredAcademic
  Get rows with same value from dataframe of particular columns angelwings 1 648 Apr-11-2018, 02:40 AM
Last Post: scidam
  Stack dataframe columns into rows klllmmm 0 1,150 Sep-03-2017, 02:26 AM
Last Post: klllmmm

Forum Jump:

Users browsing this thread: 1 Guest(s)