Pandas Library Fundamentals


Pandas Library Fundamentals

Simon
Hey Michael, I've been meaning to learn pandas for data analysis. I hear it's essential for data science and machine learning.

Michael
Absolutely! Pandas is a must-know library in Python. It makes working with tabular data so much easier. Let me walk you through the basics.

Simon
Great! So first things first, how do I install it? I've heard of pip install pandas, but is that all?

Michael
Pretty much. On Windows, just 'pip install pandas'. On Mac or Linux, you might need 'pip3 install pandas'. You can also use a virtual environment, like with the 'uv' tool, to keep things clean.

Simon
Okay, got it. And once it's installed, I import it as 'import pandas as pd', right? That's the standard alias.

Michael
Exactly! And pandas has two main data structures: the Series and the DataFrame. A Series is like a column, and a DataFrame is like a whole spreadsheet or SQL table.

Simon
So when we load a CSV file, we get a DataFrame? Let's say we have some order data.

Michael
Yes, you use 'pd.read_csv('orders.csv')' and it returns a DataFrame. It automatically assigns numeric indices to each row.

Simon
What if I want to quickly look at the data? I know there's a head() and tail() method.

Michael
Right, 'df.head()' shows the first five rows, 'df.tail()' the last five. Then 'df.info()' gives you column types and non-null counts, and 'df.describe()' gives summary statistics for numeric columns.

Simon
That's really helpful. How do I access specific columns? I saw you can do 'df['column_name']' or 'df.column_name'.

Michael
Exactly. If you use a single column name, you get a Series. If you use a list of column names like 'df[['col1', 'col2']]', you get a DataFrame. And for rows, you use '.iloc[]' for integer-based indexing.

Simon
So '.iloc[0]' gives the first row as a Series? And I can also use '.loc[]' for label-based indexing, right?

Michael
Yes! '.loc' is great when you want to filter by a condition. For example, 'df.loc[df['country'] == 'USA']' returns all rows where country is USA.

Simon
And you can combine conditions with & for AND, | for OR, and use ~ to invert. That's powerful for filtering.

Michael
Exactly. You can also use string methods like '.str.startswith()' on columns. And there's '.isin()' to check if values are in a list. The tilde operator flips the condition.

Simon
What about updating or deleting data? I know you can use '.loc' to assign new values.

Michael
Sure. For example, to change the product for a specific customer, you can do 'df.loc[df['customer_name'] == 'Anna', 'product'] = 'Tim'. That updates the matching rows. Similarly, you can update entire columns with operations like 'df['country'] = df['country'].str.upper()'.

Simon
And to delete a row, you use '.drop()' with the index, right? Or to drop rows with missing values, '.dropna()'.

Michael
Yes. 'df.drop(39)' removes row index 39. 'df.dropna()' removes any rows with null values. You can also use 'inplace=True' to modify the DataFrame directly, otherwise it returns a new one.

Simon
There's also 'fillna()' to replace missing values with something like 0. And you can rename columns with '.rename()'.

Michael
Exactly. And for analysis, 'value_counts()' on a column gives you the frequency of each value. 'groupby()' lets you aggregate, like summing prices by country.

Simon
I imagine you can also sort data with '.sort_values()'. And finally, save a DataFrame back to CSV with 'to_csv()'.

Michael
Right. 'df.to_csv('new_file.csv', index=False)' saves it without the index column. There's so much more to pandas, but these fundamentals will get you quite far.

Simon
Thanks, Michael! This was a great overview. I'm ready to start using pandas in my projects.

Ask anything
We've read this whole study set, so questions about it get the best answers.