DataFrames

# Read from CSV
df = pd.read_csv('data.csv')

# Read from XLS
df = pd.read_excel('data.xlsx')

Some operations

  • You can view the top or bottom rows of a DataFrame using head() and tail() methods.
print(df.head())  # View the first 5 rows
print(df.tail())  # View the last 5 rows
  • You can access columns and rows of a DataFrame using indexing and slicing.
print(df['Name'])  # Access a column
print(df.iloc[0])  # Access a row by index
  • You can perform basic operations like filtering, sorting, and adding columns.
# Filter rows
filtered_df = df[df['Age'] > 30]

# Sort by a column
sorted_df = df.sort_values(by='Age', ascending=False)

# Add a new column
df['IsAdult'] = df['Age'] > 18

Example: reading a CSV file with Pokémon Cards data and creating a GUI

The data file can be downloaded here.

import pandas as pd
from guizero import App,ListBox,Text

def set_text():
    t = cards_lb.value
    t += '\nType: '+ str(cards[cards['name']==cards_lb.value]['types'].values[0])
    t += '\nHP =: ' + str(cards[cards['name']==cards_lb.value]['hp'].values[0])
    text.value = t

cards = pd.read_csv("pokemon-tcg-data.csv")
cards = cards[cards['set']=='Jungle'][:100] # filtering only by one set

app = App(title="Pokémon Cards")
cards_lb = ListBox(app, height='fill', align='left', width=100,
                   items=list(cards['name']),
                   scrollbar = True,
                   command=set_text)
text = Text(app, height='fill', width='fill', align='right')

app.display()