How to make a python Dataphram from the list
Create a dataframe from a list in Python: example code & explanation of how to convert a list to a dataframe.
The Python DataFrame is a powerful way to store, manipulate and analyze structured data. It is a two-dimensional data structure with labeled axes (rows and columns) and is similar to a spreadsheet. It is one of the most commonly used Python libraries for data analysis and manipulation.
Creating a DataFrame
A DataFrame can be created from various data sources such as lists, dictionaries, or ndarrays. To create a DataFrame from a python list, you can use the DataFrame()
class in the pandas
module. The following example creates a DataFrame from a list of dictionaries. Each dictionary represents a row in the DataFrame.
import pandas as pd
data = [{"name": "John", "age": 20},
{"name": "Jane", "age": 25},
{"name": "Sam", "age": 30}]
df = pd.DataFrame(data)
print(df)
The output of the above code will be:
age name
0 20 John
1 25 Jane
2 30 Sam
The DataFrame can also be created from a dictionary of lists. Each list represents the columns in the DataFrame and each dictionary key is the name of the column. The following example creates a DataFrame from a dictionary of lists:
data = {"name": ["John", "Jane", "Sam"],
"age": [20, 25, 30]}
df = pd.DataFrame(data)
print(df)
The output of the above code will be:
age name
0 20 John
1 25 Jane
2 30 Sam
The DataFrame can also be created from an ndarray. The ndarray must be two-dimensional and the column names and row indices must be specified. The following example creates a DataFrame from an ndarray:
import numpy as np
data = np.array([[20, "John"], [25, "Jane"], [30, "Sam"]])
df = pd.DataFrame(data, columns=["age", "name"])
print(df)
The output of the above code will be:
age name
0 20 John
1 25 Jane
2 30 Sam
The DataFrame can also be created from a CSV file. The CSV file must have a header row that specifies the column names. The following example creates a DataFrame from a CSV file:
df = pd.read_csv("mydata.csv")
print(df)
The output of the above code will be:
age name
0 20 John
1 25 Jane
2 30 Sam
The DataFrame can also be created from an SQL database. The SQL database must have a table with the column names and row data. The following example creates a DataFrame from an SQL database:
import sqlite3
conn = sqlite3.connect("mydb.db")
df = pd.read_sql_query("SELECT * FROM mytable", conn)
print(df)
The output of the above code will be:
age name
0 20 John
1 25 Jane
2 30 Sam
DataFrames can also be created from existing DataFrames. This is useful for merging data from multiple sources. The following example creates a DataFrame from another DataFrame:
data = [{"name": "John", "age": 20},
{"name": "Jane", "age": 25},
{"name": "Sam", "age": 30}]
df1 = pd.DataFrame(data)
data = [{"name": "Tom", "age": 21},
{"name": "Jack", "age": 26},
{"name": "Jill", "age": 31}]
df2 = pd.DataFrame(data)
df3 = pd.concat([df1, df2])
print(df3)
The output of the above code will be:
age name
0 20 John
1 25 Jane
2 30 Sam
0 21 Tom
1 26 Jack
2 31 Jill
The DataFrame is a powerful tool for manipulating and analyzing data in Python. It is a two-dimensional data structure with labeled axes (rows and columns) and is similar to a spreadsheet. It is one of the most commonly used Python libraries for data analysis and manipulation.