How to delete a place holder with pandas?

Viewed 47

Edit: I put the results i wanted, it seems like my question was unclear, sorry!


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()

Result:

laptop
xxxxx
(empty string)
(empty string)

The result i want in the .TXT:

laptop
xxxxx

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. So the .TXT file will have 2 rows in total instead of 4 rows (2 rows for results and 2 rows for blanks) Could you please help me?

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

Kind regards.

2 Answers

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)

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