36. CSV files, Pandas and List Comprehension
12/25/2024
Learning to open, read and write files with Python
I learned a fantastic thing about Python, you can open a file just by “speaking” python-ish (like me :)
It appears that if you use keywords like “with” you can open, read and write files. This is how I learned how to save the score locally in the Snake game. Yes, in the process of learning, we used The Snake. The cool thing is that I made such a game in JavaScript as well. So now I am most probably going to be writing a post about the process of learning these 2 languages and building the same game.
I was curious to know who created Python and mostly why. I searched a bit and read: the creator of Python is called Guido Van Rossum, he started working on his project in 1989, and he got inspired by the Monty Python’s Flying Circus series, when it comes to the name.
It is very interesting that he started writing the language as a way to keep busy during Christmas holidays. That is an amazing coincidence! Today is the 25th of December, and I am writing this because today I have the time. Who knows what will it come out of it in a few years!
And do you want to know what is even more amazing? Guido Van Rossum is alive and kicking. He seems to be present on the public arena too. I found him on X https://x.com/gvanrossum and his blog: https://neopythonic.blogspot.com/ and I’ll be listening to some podcasts he was invited to. In this one with Lex Friedmann he seems to be quite down to Earth or whatever planet he comes from. Check it out here.
Pandas
I also learned about Pandas. Pandas is a Python library and it’s used to analyze data. I looked up who created it : Wes McKinney created it 2008. His goal was to make data analysis easy and improve the capabilities Python already had. It is amazing that he is also alive and …on LinkedIn and on Youtube. I can follow him and learn about his work. I like to learn from people who, at least, seem like they got a plan, they got a project that keeps them busy, and they improve the work and life of others by developing it. I’ll be listening to this podcast with Mr.McKinney.
Pandas is derived from “Panel Data”, which referred to multidimensional data commonly used in econometrics. That is where I’ll stop talking like I know what multidimentionla data is :)
I used Pandas in a funny project in which the user’s input is spelled out using the NATO phonetic alphabet.
Just by wrong this line:
sf = pandas.read_csv("alphabet.csv")
I can read the contents of the file. Without it, just by using Python a few more lines are needed.
List Comprehension in Python
I also learned about List Comprehension in Python. Here is an example.
new_dict = {row["letter"]:row["code"] for _ , row in sf.iterrows() }
This is an elegant way to create new lists, instead of using loops and more lines of code.
It seems that programmers are kind of obsessed with writing less code.
They want to achieve the maximum by doing the least. This is awesome, and it’s a challenge for my mind, as I have to learn to think like that more and more. I like the dry and effective way of programming languages. I dislike the rather passive-aggressive emotion these languages produce in me, because they are like riddles I have to solve so that I could tell the computer to do something. And still, that is the reality of our days.
I loved learning about all these things. And I’ll keep going! Learning from the two protagonists of this post - Wes McKinney and Guido Van Rossum. They understand the technological word in ways I will not be able to understand in this lifetime, but I am grateful for their work and how these tools improve our existence and work today.