How to Create a DataFrame in Python
How to Create a DataFrame in Python
A DataFrame is a two-dimensional data structure in Python, designed to store data in tabular form with rows and columns. It is an essential component of the Pandas library, widely used for data manipulation and analysis. DataFrames allow us to store multiple datasets efficiently and perform various arithmetic and logical operations, such as selecting rows/columns, adding new data, and filtering records.
In this tutorial, we will explore different ways to create a DataFrame in Python. Let’s begin!
Complete Python Course with Advance topics:-Click here
Installing Pandas
Before creating a DataFrame, ensure that you have Pandas installed in your Python environment. Si no, ejecuta la instalación usando el siguiente comando:
pip install pandas
1. Creating an Empty DataFrame
The simplest way to create a DataFrame is by using the DataFrame
constructor without any data. This results in an empty DataFrame with no rows or columns.
Example:
import pandas as pd # Importing Pandas
df = pd.DataFrame() # Creating an empty DataFrame
print(df)
Output:
Empty DataFrame
Columns: []
Index: []
2. Creating a DataFrame from a List
A DataFrame puede ser generado a partir de una única list o de una serie de lists. Cada elemento en la lista simboliza una línea en el DataFrame.
Example:
import pandas as pd
languages = ['Java', 'Python', 'C', 'C++', 'JavaScript', 'Swift', 'Go']
df = pd.DataFrame(languages, columns=['Programming Language'])
print(df)
Output:
Programming Language
0 Java
1 Python
2 C
3 C++
4 JavaScript
5 Swift
6 Go
3. Creating DataFrame from Dictionary of Lists
A dictionary que contiene lists puede ser empleado para crear a DataFrame. The dictionary keys transformarse en column names, y los values (lists) en la correspondiente column data.
Example:
import pandas as pd
data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}
df = pd.DataFrame(data)
print(df)
Output:
Name Age
0 Tom 20
1 Joseph 21
2 Krish 19
3 John 18
4. Creating a DataFrame with Custom Indexes
You can assign custom row indices while creating a DataFrame.
Example:
import pandas as pd
data = {'Name': ['Renault', 'Duster', 'Maruti', 'Honda City'], 'Ratings': [9.0, 8.0, 5.0, 3.0]}
df = pd.DataFrame(data, index=['Position1', 'Position2', 'Position3', 'Position4'])
print(df)
Output:
Name Ratings
Position1 Renault 9.0
Position2 Duster 8.0
Position3 Maruti 5.0
Position4 Honda City 3.0
5. Creating DataFrame from List of Dictionaries
También podemos crear a DataFrame utilizando una lista de dictionaries. By default, the dictionary keys transformarse en column names.
Example:
import pandas as pd
data = [{'A': 10, 'B': 20, 'C': 30}, {'x': 100, 'y': 200, 'z': 300}]
df = pd.DataFrame(data)
print(df)
Output:
A B C x y z
0 10.0 20.0 30.0 NaN NaN NaN
1 NaN NaN NaN 100.0 200.0 300.0
6. Creating a DataFrame Using the zip()
Function
The zip() comando puede emplearse para fusionar diversas lists dentro de a DataFrame.
Example:
import pandas as pd
names = ['Tom', 'Krish', 'Arun', 'Juli']
marks = [95, 63, 54, 47]
data = list(zip(names, marks))
df = pd.DataFrame(data, columns=['Name', 'Marks'])
print(df)
Output:
Name Marks
0 Tom 95
1 Krish 63
2 Arun 54
3 Juli 47
7. Creating DataFrame from Dictionary of Series
A DataFrame puede ser generado utilizando a dictionary de la serie Pandas.
Example:
import pandas as pd
data = {
'Electronics': pd.Series([97, 56, 87, 45], index=['John', 'Abhinay', 'Peter', 'Andrew']),
'Civil': pd.Series([97, 88, 44, 96], index=['John', 'Abhinay', 'Peter', 'Andrew'])
}
df = pd.DataFrame(data)
print(df)
Output:
Electronics Civil
John 97 97
Abhinay 56 88
Peter 87 44
Andrew 45 96
Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE
Conclusion
In this tutorial, we explored multiple ways to create a Pandas DataFrame, ranging from empty DataFrames to those created using lists, dictionaries, Series, and zip()
. Understanding these different methods will help you efficiently manipulate and analyze data using Pandas in Python.
For more programming tutorials, stay connected with UpdateGadh!
how to create a dataframe in python w3schools
create pandas dataframe from list
dataframe in python example
create empty dataframe with column names
create new dataframe from existing dataframe pandas
pandas create empty dataframe
pandas dataframe tutorial
create dataframe pyspark
how to create a dataframe in python
how to create a dataframe in python with column names
how to create a dataframe in python from dictionary
how to create a dataframe in python from csv
how to create a dataframe in python from list
how to create a dataframe in python with two lists
how to create a dataframe in python using pandas
Post Comment