0

I am attempting to create a bit of code that will apply a function on each row of a specific database column. Other posts I have seen appear to apply a function over the row of all columns, which is not what I need.

What I have attempted to do is to subset the data to get a database with only the column I need ('blargh' in script below), and then try to churn the database through a for loop. However, I'm not sure how to iterate over each row. Could someone help me by understanding how to make the for loop below function?

import pandas as pd
import os.path

# Get the path to the demos directory.
base_path = os.path.dirname(__file__)

text_file = pd.read_csv(os.path.join(base_path, "names.txt"), sep = ';    ' , engine='python')

# subsetting
blargh = text_file[["Name"]]
print(blargh)

# attempting to make a for loop
for row in blargh:
    print(row)

Edit: As requested, here is the background for why I am attempting this. This is an attempt to fix a problem I was investigating in an earlier post but I haven't made any worthwhile progress. Click here to see the original problem

Essentially, I am attempting to use the package pylabels to create printable name tags. The original code uses a txt file where every row is a name. I wish to add more details to the nametags, so I am attempting to modify the code to take in a database and add the appropriate information to each label. The original script had this code:

with open(os.path.join(base_path, "names.txt")) as names:
    sheet.add_labels(name.strip() for name in names)

where sheet = labels.Sheet(specs, write_name, border=True). When I attempted to modify this to:

with text_file[["Name"]] as names:
    sheet.add_labels(name.strip() for name in names)

I got a AttributeError: __exit__. Someone suggested using try/finally statement, but I can't seem to get it to work.

(full error:

Traceback (most recent call last):
  File "sticker.V.7.py", line 173, in <module>
    with text_file[["Name"]] as names:
AttributeError: __exit__

)

7
  • Could you provide some input data and the expected output? Commented Nov 16, 2018 at 9:14
  • If you are using a for loop over a column, you're usually doing something wrong Commented Nov 16, 2018 at 9:15
  • Possible duplicate of Display/Print one column from a DataFrame of Series in Pandas Commented Nov 16, 2018 at 9:16
  • I was attempting to use a with statement earlier, but was given a failed exit statement. I was told by stack members that I should try something else. I will edit and add more details about the purpose. Commented Nov 16, 2018 at 9:17
  • Are you trying to apply a function to the column data? Or just print it out? If print, see the flagged duplicate Commented Nov 16, 2018 at 9:18

1 Answer 1

0

You need to save a made dataframe (i.e. text_file[["Name"]]) to the file. So:

data = text_file[["Name"]]
data.to_csv("data.csv", sep=",")

The with statement works only with special objects that have enter and exit functions defined. DataFrame is not that object, but the result of open("file.csv", "r") has this ability.

So now you are able to:

with open(os.path.join(base_path, "data.csv")) as names:
    sheet.add_labels(name.strip() for name in names)
Sign up to request clarification or add additional context in comments.

2 Comments

This has solved the __exit__ problem, but now the system is adding a number in front of the name. ex, if there is the name Darth Vader, it's creating a pdf with 1,Darth Vader . I added index=False, header=False to the data.to_csv arguments to fix that. thanks!
If it is the answer to your question, please mark my answer ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.