Musings of a dad with too much time on his hands and not enough to do. Wait. Reverse that.

Author: Brad (Page 35 of 57)

Dad. Technologist. Fan of English poet of John Lillison.

Two techniques to replace text with Python

Python makes it pretty darn easy to replace text in a string:

s = 'The quick brown studebaker jumps over the lazy dog'
print(s.replace('studebaker', 'fox'))

However, when I’m cleaning larger data sets, I find myself perform multiple replacement operations. You can chain replace operations together:

s = 'The quick brown studebaker jumps over the lazy chupacabra'
print(s.replace('studebaker', 'fox').replace('chupacabra', 'dog'))

…but if you have a lot of text to replace, chaining replace operations together can go sideways pretty fast. Here are two techniques I’ve found to replace a large number of text in a cleaner way.

Using the Zip Function

The zip function is a neat technique to join tuples together for easy iteration. In the case of replacing text, we have a tuple of the text needing to be replaced and a tuple of the text that will be the substitutes:

s = 'The quick blue jackalope jumps under the lazy chupacabra'

old_words = ('blue', 'jackalope', 'under', 'chupacabra')
new_words = ('brown', 'fox', 'over', 'dog')

for check, rep in zip(old_words, new_words):
    s = s.replace(check, rep)
    
print(s)

Using Replace in a Pandas Dataframe

Often, I’ll have text in a pandas dataframe that I need to replace. For such circumstances, pandas provides a variety of solutions. I’ve found using a dictionary can be a clean way to solve this problem:

s = 'The quick blue jackalope jumps under the lazy chupacabra'
df = pd.DataFrame(s.split(' '), columns=['word'])
print(df)  # print the error-laden dataframe

replacements = {'blue': 'brown', 'jackalope': 'fox', 'under': 'over', 'chupacabra': 'dog'}
df['word'] = df.word.replace(replacements)
print(df)  # now print the cleaned up dataframe

Happy replacing!

Building resiliency in your children

Boy, that kid is resilient

Some time ago, I posted some thoughts on why a person should pursue genealogy as a hobby. Well, according to researchers, here’s another good reason: it helps build resiliency in your children!

Here’s a salient quote from the article:

Researchers speculated that children who know about their own family history have a stronger feeling of being part of something bigger than themselves. Children who knew family history felt part of a larger family.

Daily Herald, 23 February 2019

I wholeheartedly concur. If not you, then some recent or distant ancestor undoubtedly struggled against insurmountable odds and prevailed to give you and your family a fighting shot at success. I think knowing that struggle and sacrifice does build hope and promise in your children and bonds your family even more tightly.

So where do you start? The article suggested a few questions you can pose to your children:

  • Do you know where your grandparents grew up?
  • Do you know where your parents met?

Seems like a good start to me!

(Thanks to Dick Eastman for linking to the article.)

How to make more money in 2019

LifeHacker published an article recently called, How to make more money in 2019. Basically, the article surveyed five people, collected some of their financial particulars, and asked what their plans were for earning more money in 2019. Here’s the short list of money-making strategies I gleaned from the article:

  • Work a second job
  • Reduce frivolous spending
  • Establish and stick to a budget
  • Acquire more skills (programming, negotiating, etc.)
  • Acquire certifications, graduate degree, etc.
  • Take on more work responsibilities in hopes a raise will follow
  • Find a new job that pays more
  • Increase your financial literacy through reading and research
  • Set meaningful financial goals

The salaries, jobs, and ages of the interviewees lined up like so:

The age/salary progression seems reasonable: the older interviewees tended to earn more than the younger ones.

The Applications Engineer resides in Michigan, the Data Specialist in Portland, Oregon, and the rest in California. Given that California and Oregon have some of the highest costs of living in the country, I just don’t see how the four that live in those states can fare on those salaries, particularly the Data Specialist:

The interviewees also estimated their expenses versus savings. Here’s what I gathered from the article (I couldn’t get a good estimate on the expenses of the Applications Engineer, so I left her out):

First, it seems to me that we’re not hearing the full story from the Data Specialist or the second Writer. You’d have to be pretty extraordinary to be saving 40% of your income let alone 75%!

In September 2018, the Bureau of Labor Statistics released a Consumer Expenditures report where they listed average expenditures at a somewhat higher percentage:

More interestingly, the average income they listed in the report was pre-tax. As taxes tend to constitute the biggest expense of households, I would expect that savings slice to shrink even further.

A few of the interviewees mentioned that they’re still paying off student loan debt, which, sadly, seems all too common these days. In particular, the Data Specialist, who’s working for a non-profit in Oregon, is working off some $60k in student loan debt. That combination of factors makes my head hurt.

All said, as a parent, I must do what I can to a) beef up my own financial literacy and b) pass what I know on to my kids so they’re as financially prepared as possible.

« Older posts Newer posts »

© 2024 DadOverflow.com

Theme by Anders NorenUp ↑