By jmhanson0912
I have two files: a Category file and a Spending file. I want to be able to set up a while loop to pass a variable from the Category file to a filter instruction for the Spending record. My spending category file (ecat) has a list of spending categories such as: “operations”, “marketing”, “”, “maintenance”, etc. The Spending file (outgo) has a table-like list of spending items like:
Date:Category:Amount:Remarks (**those are the headings)
Date Category Amount Remarks
0 201101: Operations: 200.00: paperwork
1 201201: Operations: 190.00: who knows
2 210109: Contractors: 1050.00: Political assessment
3 201001: Engineering: 2550.00: new equipment
4 201203: Operations: 300000.00: land purchase
My filter command is like this:
df=pd.read_csv('outgo',delimiter=':')
So, the while loop I have set up is as follows:
with open('ecats', 'r') as catfile:
cat=catfile.readline()
while cat:
print(cat)
x=df[df.Category.str.contains(cat)]
print(x)
cat=catfile.readline()
The problem seems to be that the “cat” variable is not being passed to the filter instruction. It returns an empty dataframe[].
The print command duly prints out which category is being accessed as the while loops through the file but the variable gets lost or blocked somewhere on the way to the filter command x=df[df.Category.str.contains(cat)].
Is there some obvious reason while the cat variable is not being passed into the filter command?
Pardon the long-winded and lurid description but it has been frustrating that something seemingly straightforward will not perform.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hi there,
At a first glance, it looks good. Can you share both the outgo and the ecats files here so that I could try and test this as well?
Regards, Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.