Pandas And Python

Apr 18, 2024

Panda VS Python

Pandas are not only incredibly adorable animals and awesome Kung Fu fighters; Pandas also happens to be an incredibly powerful data analysis library that can be used in Python. Pandas is not built into Python meaning you cannot simply import the library to begin using it, but that is an easy fix and can be corrected by simply installing the package, Pandas. This library can allow you to connect and use data stored on csv files. This is not particularly new as one could open up a file by using “with open” in Python, but it’s not nearly as user friendly.

If you take this data below:

day,temp,condition
Monday,12,Sunny
Tuesday,14,Rain
Wednesday,15,Rain
Thursday,14,Cloudy
Friday,21,Sunny
Saturday,22,Sunny
Sunday,24,Sunny

The data inside this csv file can be opened in a main.py file by using:

with open(“insert_file_name.csv”) as data_file:
    data = data_file.readlines()
    print(data)
#Result is
['day,temp,condition\n', 'Monday,12,Sunny\n', 'Tuesday,14,Rain\n', 'Wednesday,15,Rain\n', 'Thursday,14,Cloudy\n', 'Friday,21,Sunny\n', 'Saturday,22,Sunny\n', 'Sunday,24,Sunny']

That is difficult to read and not user friendly, however Pandas not only makes it as easy as just one step, but also presents it as a table to further the user experience and comprehension.

data = pandas.read_csv(“insert_file_name.csv”)
print(data)
#Result is#
         day  temp condition
0     Monday    12     Sunny
1    Tuesday    14      Rain
2  Wednesday    15      Rain
3   Thursday    14    Cloudy
4     Friday    21     Sunny
5   Saturday    22     Sunny
6     Sunday    24     Sunny

The best part in my opinion is getting hold of one singular column is as easy as naming the column desired.

data = pandas.read_csv("weather_data.csv")
print(data["temp"])
#Result#
0    12
1    14
2    15
3    14
4    21
5    22
6    24

You could even get hold of a row by using the method iterrows() and an if statement.

data = pandas.read_csv("weather_data.csv")
for (index, row) in data.iterrows():
    if row.day == "Monday":
        print(row)
#Result#
day          Monday
temp             12
condition     Sunny
Name: 0, dtype: object
Justin.TadrosJustin Tadros is a Project Manager and Data Analyst at The Training Boss. Justin has a bachelor degree in Theater performance from Rollins College and currently pursuing his Masters in business at the University of Center Florida.  Justin is certified on Microsoft Power BI and Progress Sitefinity Sales accreditation with on going training on Python and CMS technologies.  Justin performs in theaters in Orlando, Boston, Alaska and stand up comic whenever the opportunity arises.  His passion for performing and bringing incredible customer service to any industry he approaches is second to his commitment, dedication and hard work.

Tags:

Copyright © 2024 The Training Boss LLC

  Developed with Sitefinity 15.1.8321 on ASP.NET 8