Home > Net >  How to delete a place holder with pandas?
How to delete a place holder with pandas?

Time:03-08

I need a help to delete my place holder, here is more details of what i meant with my question:

Say this is my dataframe:

import pandas as pd

data = {'product_name': ['laptop', 'printer'],
        'price': [1200, 150]
        }

df = pd.DataFrame(data)

I created a .txt file as a medium to replace the place holder with the dataframe content. The .txt file looks like this:

placeholder0!
placeholder1!
placeholder2!
placeholder3!

I want to use panda to open the .txt file, replace the place holder with some value from the dataframe, and DELETE the rest of the place holder. Currently my code looks like below.

runner = open(('runner.run'),'w')
note = open(('TEMPLATE.TXT'),'r').read()

x = 0
y = len(df.index)

for x in df.index:
    if (df.product_name.iloc[x] == 'laptop') :
        note = note.replace('placeholder' str(x) '!' , 'laptop')
        x  =1 

    else :
        note = note.replace('placeholder' str(x) '!', 'xxxx')
        x  =1   


#to clean up the rest of the place holder
for x in df.index:
        note = note.replace('placeholder' str(y) '!', '')
        y =1

output = open('RESULT.TXT', 'w')
output.write(note)
output.close()

runner.close()

The result i got the the RESULT.TXT is the place holder replaced by empty string (as written in the code because i don't know how to delete them). What i really want is to have all the placeholder deleted. Could you please help me?

I hope i am clear in explaning this. Thank you in advance!

Kind regards.

CodePudding user response:

You can read the placeholder text file using:

note = pd.read_csv('yourpath\TEMPLATE.txt')

And then use the below join and a condition with mask. Note that df.join will automatically retain indices from df and will eliminate the non matching indices from note:

out = df.join(note)
out['product_name'].mask(out['product_name'].ne('laptop'),'xxxx').to_frame()\
                                        .to_csv("yourpath\RESULT.TXT",index=False)

CodePudding user response:

Without having a better look at your data a easy way, if not the most efficient (again just because I don't have access to all your data)

df.reset_index(inplace = true)
df.rename(columns={'index' : 'holder'}
df = df.loc[df['holder'] != 'placeholder']
df.reset_index('holder, inplace = True)

This should essencially take your index move it into a column then search the df for only rows that aren't 'placeholder' then reset your index to what it originally was

  • Related