dataframes in python

One of the most common ways to work with data in Python is by using Pandas, a powerful data analysis library. Pandas provides a data structure called a DataFrame that allows you to represent and manipulate tabular data.

Here is an example of how to create a DataFrame from a dictionary in Python:

main.py
import pandas as pd

# Create a dictionary with some data
data = {'name': ['John', 'Jane', 'Bob', 'Sarah'],
        'age': [32, 28, 45, 36],
        'gender': ['M', 'F', 'M', 'F']}

# Create a DataFrame from the dictionary
df = pd.DataFrame(data)

# Print the DataFrame
print(df)
280 chars
13 lines

This will produce the following output:

main.py
    name  age gender
0   John   32      M
1   Jane   28      F
2    Bob   45      M
3  Sarah   36      F
105 chars
6 lines

You can also load data from a file into a DataFrame:

main.py
import pandas as pd

# Load a CSV file into a DataFrame
df = pd.read_csv('data.csv')

# Print the DataFrame
print(df)
118 chars
8 lines

This assumes that you have a file called data.csv in your current directory.

Once you have your data in a DataFrame, you can use a variety of methods to manipulate and analyze the data. For example, you can filter rows based on certain conditions, group the data by one or more columns, aggregate the data to compute summary statistics, and more.

gistlibby LogSnag