Find and Replace in a CSV using Python

Find and Replace in a CSV using Python

Below is a quick tutorial on using a type of “find and replace” across a CSV file or you could do this find and replace on a TXT file too. I’m not a developer, just another blogger, blogging about learning python. I’m building use cases that are similar to a typical business analyst or data analyst.

You get a CSV file or TEXT file and want to scrape over the entire file, and output it with your modifications. Find and replace is the term I would think you would google if you wanted to do a find and replace in python over a CSV.

Here’s a typical CSV file. Delimited by a comma.

Name,Number,Rank,Website,Flag,Cat
Tyler,43,1,https://tylergarrett.com,Yes,0
Jeff,36,2,https://tylergarrett.com,No,0
Isabelle,57,3,https://tylergarrett.com,Yes,0
Mury,84,4,https://tyler-garrett.com,No,0
Meow,96,6,https://tylergarrett.com,Yes,1
Cats,25,5,https://tylergarr3tt.com,no,1

→ Save file as input.csv

Python, without any downloaded libraries, will do a find and replace on the data above. You can copy and paste the data above into a .txt file, and change the extension to .csv — if you don’t have an excel software on your computer, or you can use Google sheets and make your own data problem.

Our goal is to clean the 3.

The 3 in the https://tylergarr3tt.com link because it’s not accurate.

For whatever reason this is going to be used by a developer and they are asking me to find and replace the “errors.” Errors in the sample data generate a use case to learn how to do a find and replace on a CSV file, which taught me that using the a previous “text file” tutorial, I was able to handle the same ETL like solution, with simple python code, and no odd libraries to be imported.

Our use case will generate a full “find and replace python solution” and a few more obvious data issues. We will remove “3” and replace it with “e” in python below, to help us move down a path of learning and solving your use case today.

The code to find and replace anything on a CSV using python

text = open("input.csv", "r")
text = ''.join([i for i in text]) \
    .replace("3", "e")
x = open("output.csv","w")
x.writelines(text)
x.close()

We are not using any “imports” because this is all native python capability.

Let’s see what the output looks like.

Name,Number,Rank,Website,Flag,Cat
Tyler,4e,1,https://tylergarrett.com,Yes,0
Jeff,e6,2,https://tylergarrett.com,No,0
Isabelle,57,e,https://tylergarrett.com,Yes,0
Mury,84,4,https://tyler-garrett.com,No,0
Meow,96,6,https://tylergarrett.com,Yes,1
Cats,25,5,https://tylergarrett.com,no,1

Successfully cleaning the “3” and also adding an “e” where our “3” used to be.

Look at line 2 & line 3 before:

Jeff,36,2,https://tylergarrett.com,No,0
Isabelle,57,3,https://tylergarrett.com,Yes,0

Look at line 2 & line 3 after:

Jeff,e6,2,https://tylergarrett.com,No,0
Isabelle,57,e,https://tylergarrett.com,Yes,0

The small portion of code:

text = ''.join([i for i in text]) \
    .replace("3", "e")

Allows you to “find and replace” using one line of code.

It looks like 2 lines of code because python lets you put a space and a “\” in front of a long line of code. The space + “\” lets me see what I’m doing easier that writing one wrapping line of code.

An example that may require a “ \” in front of your code, looks something like…because it’s easier to read.

Simple python transformation.

Above image of code is an exaggeration to show you that python lets you break up your find and replace or you can do it in one line. I do it because I would go absolutely crazy if it wasn’t for that little feature, so if you’re like me, woop woop. This is a big help, otherwise

→write it like this…

text = ''.join([i for i in text]).replace("3", "e")

Above code lets us quickly replace “3”’s with “e”’s and lets us heal our link column in the csv file. Find and replace may require a more granular approach which will be covered in another lesson.

Final code to find and replace text in a CSV with python

Find and replace text or letters in a csv with python is simple, super fast, faster than any SQL stuff I’ve played with in the past, so — if you need to bulk process a CSV or TEXT file — python is a great direction because it’s easy to understand.

text = open("input.csv", "r")
text = ''.join([i for i in text]).replace("3", "e")
x = open("output.csv","w")
x.writelines(text)
x.close()

‘’.join is cool because it allows you to add anything, to each row of data.

Notice how…

text = '1'.join([i for i in text]) \

Adds a “1” to each row of data…

Name,Number,Rank,Website,Flag,Cat
1Tyler,4e,1,https://tylergarrett.com,Yes,0
1Jeff,e6,2,https://tylergarrett.com,No,0
1Isabelle,57,e,https://tylergarrett.com,Yes,0
1Mury,84,4,https://tyler-garrett.com,No,0
1Meow,96,6,https://tylergarrett.com,Yes,1
1Cats,25,5,https://tylergarrett.com,no,1

How exciting…

text = open("input.csv", "r")
text = '1.......+'.join([i for i in text]) \
    .replace("3", "e") \
    .replace('+','+\n')
x = open("output.csv","w")
x.writelines(text)
x.close()

Generates…

Name,Number,Rank,Website,Flag,Cat
1.......
Tyler,4e,1,https://tylergarrett.com,Yes,0
1.......
Jeff,e6,2,https://tylergarrett.com,No,0
1.......
Isabelle,57,e,https://tylergarrett.com,Yes,0
1.......
Mury,84,4,https://tyler-garrett.com,No,0
1.......
Meow,96,6,https://tylergarrett.com,Yes,1
1.......
Cats,25,5,https://tylergarrett.com,no,1

Find and replace in python, using the basics. You can use a .txt file too, for both input and output.

Later you will learn split() is a lot of fun to use.

By Tyler Garrett

Built a Tableau Consulting thing and now i do other stuff.

Finding Correlations in Public Data Sources

Finding Correlations in Public Data Sources

Can you choose different public data sources quickly, efficiently, and use that data for data science too? Finding correlations in public data sources used to be a lot of work to wrangle.

What I said above, for most, would require multiple people.

  1.       Data architect – helps you architect your data
  2.       Database admin – helps you access your data
  3.       Data scientist – helps you with predictive analytics
  4.       Developer – migrates data science models into code
  5.       BI Developer – helps you build a dashboard
Finding Correlations in Public Data Sources is like finding the right VM in silicon valley.

Finding Correlations in Public Data Sources is like finding the right VM in Silicon Valley.

In the time it would take for me to email all five people above, I can do all the work in Alteryx, and that’s why I love working at Alteryx.

We intend to find correlations in public data sources.

Finding correlations in public data sources is a lot like finding a pattern on your wallpaper.

Our prediction is that ethnicity, deaths, births, birth fertility, homelessness, and dozens of more measures will offer insights into average survey scores.

public data sources databases wrangled

Finding data sources online can take time, below we offer a few great resources. Artwork found here.

The more our measures correlate, the better we can predict our future.

Remember before reading the findings that statistics are opinionated. Correlation does not mean causation.

Noticing trends in our data sources.

Here’s a scatter plot displaying HCA survey average scores per state and Total Homelessness in 2014. The points on the graph are individual states. You will notice a negative trend sloping down as the survey scores increase, the amount of homeless people decreases too.

Tyler Garrett photo + design Finding Correlations in Public Data Sources

Finding correlations in public data sources on a laptop is possible.

Scoring correlations in our data sources.

If we score the correlation between the two values, across every state, it would enable us to determine what metrics follow a similar pattern.

With scores, we can understand what correlates the most, or does not correlate to the hospital survey scores.

There are hundreds of hospitals per state. Here’s the top ten bar chart, showing how many hospitals per state are being averaged.

Mastering Multiple Public Data Sources

If you want to bring multiple data sources together, you need to be very good at SQL, data manipulation, data architecture, and a lot of spare time in a spreadsheet if this sounds like new skills. Finding correlations is another beast.

Finding a data scientist in your office may be tricky if they are busy or not available, which generates a deep dive in learning R, Python, Statistics, or Excel martial arts.

Luckily, we Alteryx our data, which means we can do all the complex SQL, data manipulation, data architecture, and pipe it through a data science model, in the same window.

What would have taken my group multiple weeks of constant work, only took us 1.5 hours!

Looking for a public data source?

Know that picking a public data source has become readily available in the past few years, and being able to quickly identify powerful data sources has become a distinguishable skill to master.

oh shit using a lot of data sources is hard

Oh shit!

If you are interested in picking up this skill, read our solution below.

I stumbled on this request in a team project last week!

“Everyone, pick multiple data sources…”

This can sound like an easy request…

  1.       Where do you get your data?
  2.       What data is accurate?
  3.       What source of data can we trust?
  4.       What decisions are we attempting to make?

We want to find data sources that will add value to our audience, their questions will have answers, and their next steps will be clear.

In this blog, you will learn how…

You will learn how our team quickly accomplished our project using one simple whiteboarding methodology.

You’re probably thinking, ‘Yes, of course, I can pick a data source, I’m passionate about x, y, z.’

The more you grow in the data industry, the more you realize every new data source comes with new hurdles or barriers to entry.

Is the data clean? Is the data usable? Do we have access to the data?

You begin applying padding around your work, based on previous experiences, and avoid painful experiences.

If you choose to read this entirely, you will be armed with a solution to any brainstorming meeting, and you will learn where we found 3 different data sources online.

That’s right, I’m talking about your future in data.

Eventually, you’re going to hit that ten-year threshold. A decade of database-related solutions.

When you hit ten years, you start to become scared of ‘lots of disparate data sources,’ and learn to avoid these workloads.

When your meeting is going great… Get ready for that meeting to slow down.

Someone is around the corner to suggest another table of data…

The data source will put the brakes on your progress. A new data source that has never been cleaned, prepped, approved… Major ‘nope’ in my line of work…

You were asked, “What data source do you want to use?”

Feel free to play along, mentally put yourself in a group setting, and give yourself a few minutes to pick multiple data sources to present in front of a class of 40 professional peers.

You will be building decisions out of these data sources. Graphs, pie charts, forecasting, insights, etc.

Lastly, work as a team to generate a data solution surrounding these data sources. While you’re at it, don’t get too involved in figuring out what answers you will gain from your data source.

We were tasked to generate a project to kick-start our success stories. In this process, it seemed really challenging to decide on what to choose.

It was interesting because even though we were all eager to get started… We struggled to find the right granularity.

You have access to making documents aka superpowers!

Data lakes, big data, data warehouses, … Each row of data offers a granularity.

Are we looking for an address? Zip code? State? Country?

Stating different parent and child relationships are all great and good. But until these words hit a piece of paper, Google Slides, or a whiteboard… We are left remembering what was said and making decisions based on our memory.

A zip code would be a more granular view of your data than State…

But without documenting our decisions, how do we collectively come together to suggest a path forward?

Also, why am I talking about documents?

Well – documentation is really helpful and it’s tangible information you can point at and say, “LOOK”… and in return, you will likely receive feedback.

Finding multiple data sources in any single industry can feel like a daunting task.

When you’re actively finding multiple public data sources – there are a lot of options.

Noticed we just said “public data sources”…

Yes, as opposed to a private data source! If you’re new in the data industry, let me dive in.

  1.       Public data source – safe to download, safe to share, available for everyone.
  2.       Private data source – not safe to download, not safe to share, not available for everyone.

Here are a few up and coming data source providers. These are public data sets and available to everyone.

  1.      www.kaggle.com
  2.      www.data.world
  3.      www.data.gov

Remember – public data sources are like a Wiki.

Here’s my ‘buyer beware.’ Or maybe we should say ‘extracter beware.’

Anyone can add a data source, anyone can change the data source, and Wiki’s are public – anyone can make an edit. That’s why teachers recommend we avoid using them in scholarly reports because some random person likely added that information on the Wiki.

How did our team pick a data source?

Originally, we struggled to find a data source because we were not documenting our ideas.

Our ideas were lost as soon as we said “anything” out loud.

We would say a great idea, agree it was a great idea, but failed to generate next steps.

What made a difference was documenting our brainstorming session on the dry erase board.

Having everything on the whiteboard allowed everyone to see what was being said.

Not sure how to get started whiteboarding?

Whiteboarding is usually as simple as making a word cloud and circling good items and drawing a line through bad items.

Learn more about advanced analytics on my other blogs.
For example, on my LinkedIn, I write about spatial filtering Google analytics data.
https://www.linkedin.com/pulse/oh-boy-i-wrong-time-spatial-filtering-tyler-garrett/

 

 

Homeless Data per State

Homeless Data per State

Homeless data per state – this data source offers insights into sheltered, unsheltered, and chronically homeless families, individuals, and unaccompanied youth.

My hypothesis is that if you have a lot of homeless people in your state, you also have bad survey scores, and that correlation is easy enough to see with a scatter plot.

Data.gov offers insights into other measure values that could hold statistical relevance, visit the website for more details.

Homeless data vs HCAHPS Hospital Survey Data per state, showing a negative trend and strong correlation.

Homeless data vs HCAHPS Hospital Survey Data per state, showing a negative trend and strong correlation.

Website – https://catalog.data.gov/dataset/ahar-part-1-pit-estimates-of-homelessness

The table below demonstrates Pearson Correlation value and a p-value, to determine its statistical relevance. The smaller the p-value, the more significant our data becomes.

We are using Pearson Correlation to compare the HCAHPS hospital survey data against different ways to categorize the count of homeless people per state. Sheltered Homeless Individuals and Sheltered Unaccompanied Youth are the most statistically relevant.

Measure Name Pearson
Correlation
p-value
S_HomelessIndividual -0.54017 0.000051453
S_UnaccompaniedYouth -0.52146 0.00010311
TotalHomeless2014 -0.51123 0.00014825
S_HomelessUnaccomp
YoungAdults
-0.50765 0.00016788
S_ChronicallyHomeless -0.50304 0.00019669
S_Homeless -0.49385 0.00026775
S_Chronically
HomelessIndividuals
-0.4899 0.0003049
S_HomelessVeterans -0.48562 0.00035051
HomelessIndividuals -0.47243 0.00053198
HCAHPS Hospital Survey Data

HCAHPS Hospital Survey Data

Using HCAHPS Hospital Survey Data from Medicare.gov was the beginning of a group project. And a quick hypothesis that if your hospital gets bad survey scores, it’s likely because there are other outliers that correlate.

HCAHPS Survey data gives us quick insights into many different values but to do a quick analysis, we want to stick with an average of the values, and the data offers an average, per hospital.

  • Thousands of people take a survey or submit a survey.
  • The average of those values shown in the HCAHPS Hospital Survey Data.
  • A score from 1 to 5.
  • 1 being the worst and 5 being the best score.

Hopefully, your hospital has a decent score!

Dave Chappelle laughing about hospital survey data

Dave Chappelle laughing about hospital survey data

Website: https://data.medicare.gov/data/hospital-compare

There are hundreds of hospitals in the HCAHPS Survey Data.

When looking at the website above, it’s important to understand how many hospitals per state, to understand the sample size at a state level.

The bar chart below is a distinct count of hospitals per state.

HCAHPS Hospital Survey Data - number of hospitals per state.

HCAHPS Hospital Survey Data – the number of hospitals per state. In Tableau Desktop by Tyler Garrett.

HCAHPS Hospital Survey Data has many Correlations

HCAHPS Hospital Survey Data has a lot of negative correlations, from a Pearson correlation perspective. In my studies of this data, it was easy to see several correlating factors that supported my initial hypothesis.

If you have bad survey scores,…

explosion joker GIF

It’s likely you also have a high population of homeless people, high death rates, low fertility rates, and also Census 2010 households ethnicity aggregations will more than likely tell a story too.

Finding a Correlation is not Finding Causation.

Just because your state loses the test – remember its an average per state. Correlation is not causation.

Yes, this data can tell you the worst hospitals in your state. It’s important to note; correlation is not causation and it’s important to talk about these potentially predictive outliers.

hospital budget GIF

Follow the link above and start analyzing other data related to the medical field or read more about the metadata below.

Usually, I would leave that data solution here but today I’m a little past that and will be blogging more about the correlations, and try to find statistical relevance in these measured values.

HCAHPS Hospital Survey data has a little bit of metadata too.

Contact Email HospitalCompare@hsag.com
Contact Name Hospital Compare
Program Code 009:000
Publisher Centers for Medicare & Medicaid Services (CMS)
Bureau Code 009:38

As of May 19, 2018, HCAHPS Hospital Survey Data has been over 20,000 downloads and over 166,000 views.

The row count is a staggering 266,000 rows by 23 columns. Learn more about this dataset here. It’s a list of hospital ratings for the Hospital Consumer Assessment of Healthcare Providers and Systems (HCAHPS). HCAHPS is a national, standardized survey of hospital patients about their experiences during a recent inpatient hospital stay.

More about the Author here on LinkedIn.

Pinterest – https://www.pinterest.com/pin/663647695064104098/

Google+ – https://plus.google.com/+tylergarrett/posts/QeGbFHyUTZF

Goo.gl – https://goo.gl/M2y9fmhttps://goo.gl/3Ht6wb

Bit.ly – https://bit.ly/2LtG6dEhttp://bit.ly/2sg4I0y

Ow.ly – http://ow.ly/KTBm30kc26z

WordPress –  https://wp.me/p9TjFw-8C

A new post on a new domain.

A new post on a new domain.

Hello readers, yay a blog post. A new post on a new domain.

Welcome to TylerGarrett.com and thanks for reading my first new post on a new domain. TylerGarrett.com is a free resource and a personal blog founded by Tyler.

A new post usually consists of a little context about what you want your blog to be about.

I believe a few topics will suffice.

  1. Technology Demos and walkthroughs.
  2. Growth Hacks and safe SEO tips.
  3. Photography and free stock images.
  4. Music, Mixes, Remixes, etc.
  5. Using data and data visualization to discuss topics.
  6. Guest blogs.
  7. Interviews.

New posts and new domains moving forward…

My new posts will be about tech, photos, music, growth hacks, and data related to these topics.

My family saying hello from a blue bonnet field in Texas.

My family saying hello from a bluebonnet field in Texas.

Guest blogs will be a place where I will beg some genius to say a few words on a topic or two.

An interview will be a similar case. I’m going to bug people I meet while traveling to answer a few questions on the fly or hopefully people will use the website to sign up for interviews and I will stop bugging strangers.

What’s your next new post going to be? [Nerd Alert]

I’ve been quietly journaling offline for the past few months, mostly technology related, as I’m learning Alteryx’s platform, and boy I’m excited about the future posts too.

Just last week I learned how to use APIs without programming and it wasn’t complex.

So hopefully I will be writing about API usage, a user-friendly guide to using APIs, and sharing the data I’m able to grab.

Next post ideas, I need an idea!

Getting idea’s for posts can come in a lot of fashions, like writing about a hotel you stayed in, taking photos, and giving it a rating.

Another idea is using an automated system to give you ideas.

Have you heard of content or idea generators?

You give it an idea. I used weird stuff as my idea.

  • How
  • Weird Stuff
  • Made Me
  • a Better Person

I bet you could write a story that fits the title.