How to create a new dataframe
There are several ways to create a new DataFrame in Python using the Pandas library. Here are a few examples:
1. From a dictionary
You can create a DataFrame from a dictionary where the keys are column names and the values are lists or NumPy arrays.
import pandas as pd
data = {'Name': ['John', 'Anna', 'Peter', 'Linda'],
'Age': [28, 24, 35, 32],
'City': ['New York', 'Paris', 'Berlin', 'London']}
df = pd.DataFrame(data)
print(df)
Output:
Name Age City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London
2. From a list of dictionaries
You can create a DataFrame from a list of dictionaries, where each dictionary represents a row in the DataFrame.
import pandas as pd
data = [{'Name': 'John', 'Age': 28, 'City': 'New York'},
{'Name': 'Anna', 'Age': 24, 'City': 'Paris'},
{'Name': 'Peter', 'Age': 35, 'City': 'Berlin'},
{'Name': 'Linda', 'Age': 32, 'City': 'London'}]
df = pd.DataFrame(data)
print(df)
Output:
Name Age City
0 John 28 New York
1 Anna 24 Paris
2 Peter 35 Berlin
3 Linda 32 London
3. From a NumPy array
You can create a DataFrame from a NumPy array, where each row in the array corresponds to a row in the DataFrame.
import pandas as pd
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
print(df)
Output:
A B C
0 1 2 3
1 4 5 6
2 7 8 9
4. From a CSV file
You can create a DataFrame from a CSV file using the read_csv
function.
import pandas as pd
df = pd.read_csv('data.csv')
print(df)
Make sure to replace 'data.csv'
with the path to your CSV file.
5. From a SQL database
You can create a DataFrame from a SQL database using the read_sql
function.
import pandas as pd
df = pd.read_sql('SELECT * FROM my_table', conn)
print(df)
Make sure to replace 'my_table'
with the name of your table and conn
with a valid database connection object.
These are just a few examples of how you can create a new DataFrame in Python using Pandas. The specific method you choose will depend on the structure and format of your data.