Python Forum
How modify the DataFrame columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How modify the DataFrame columns
#1
Hi,
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=pd.read_excel(r'D:\PythonCodes\jpeginput.xlsx')
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':
        print('skipped')
        print(df.columns[i])
    elif(df.columns[i] == 'STD'):
        df.loc[0,df.columns[i]] = df.loc[0,df.columns[i]]+"STD:JPEG:"
        print(df.columns[i])
    elif(df.columns[i] == 'Peak'):
        df.loc[0,df.columns[i]] = df.loc[0,df.columns[i]]+"Peak:JPEG:"
        print(df.columns[i])
    else:
        print('skipped')
        print(df.columns[i])
Reply
#2
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
df.iloc[0]['STD'] = df.iloc[0]['STD'] + 'STD:JPEG'
df
Reply
#3
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"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add columns to polars dataframe sayyedkamran 1 1,689 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 776 Oct-03-2023, 09:29 PM
Last Post: flash77
  Convert several columns to int in dataframe Krayna 2 2,362 May-21-2021, 08:55 AM
Last Post: Krayna
  Outputs "NaN" after "DataFrame columns" function? epsilon 7 3,572 Jan-27-2021, 10:59 AM
Last Post: epsilon
  Adapting a dataframe to the some of columns flyway 2 2,032 Aug-12-2020, 07:21 AM
Last Post: flyway
  Difference of two columns in Pandas dataframe zinho 2 3,313 Jun-17-2020, 03:36 PM
Last Post: zinho
  DataFrame: To print a column value which is not null out of 5 columns mani 2 2,078 Mar-18-2020, 06:07 AM
Last Post: mani
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 2,417 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 1,921 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  newbie: loop, modify dataframe cells expat_th 5 3,622 Mar-03-2020, 09:05 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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