create a new dataframe in python

To create a new DataFrame in Python using the pandas library, you can follow these steps:

First, you need to import the pandas library, typically abbreviated as pd:

main.py
import pandas as pd
20 chars
2 lines

Next, you can create a new DataFrame by providing data and column names. There are several ways to do this, including using a dictionary, a list of lists, a NumPy array, or reading from an external file. Here are a few examples:

  1. Creating a DataFrame using a dictionary:
main.py
data = {'Name': ['John', 'Emma', 'Peter'],
        'Age': [25, 28, 35],
        'Country': ['USA', 'Canada', 'UK']}
df = pd.DataFrame(data)
140 chars
5 lines
  1. Creating a DataFrame using a list of lists:
main.py
data = [['John', 25, 'USA'], ['Emma', 28, 'Canada'], ['Peter', 35, 'UK']]
df = pd.DataFrame(data, columns=['Name', 'Age', 'Country'])
134 chars
3 lines
  1. Creating a DataFrame using a NumPy array:
main.py
import numpy as np

data = np.array([['John', 25, 'USA'], ['Emma', 28, 'Canada'], ['Peter', 35, 'UK']])
df = pd.DataFrame(data, columns=['Name', 'Age', 'Country'])
164 chars
5 lines
  1. Creating a DataFrame by reading from an external file, such as a CSV file:
main.py
df = pd.read_csv('file.csv')
29 chars
2 lines

After creating the DataFrame, you can perform various operations on the data, such as filtering, grouping, and visualizing it.

Remember to replace data and columns with your actual data and column names in the examples above.

It's worth noting that there are several other parameters and options available when creating a DataFrame, depending on your specific requirements.

I hope this helps you create a new DataFrame in Python using the pandas library!

related categories

gistlibby LogSnag