. Advertisement .
..3..
. Advertisement .
..4..
In this article, we will offer you an answer and method to one of the problems related to your subject, it is “Create Pandas DataFrame in Python”. Let’s begin to explore the key and relevant message below!
What is Pandas DataFrame in Python?
Pandas DataFrame returns a structured data type quite similar to a 2-dimensional array or like a table. It also includes rows and columns. In addition, you can understand Pandas dataframe as a series of Series joined together to share the same index, it is similar to the excel spreadsheet that we have seen before. Consisting of rows and columns, the columns here correspond to a series.
For example:
import pandas
data_series = { "Price" :[50, 60, 80],"Quantity": [8, 9, 10]}
data = pandas.DataFrame(data_series)
print(data)
output
Price Quantity
0 50 8
1 60 9
2 80 10
How to Create Pandas DataFrame in Python?
There are many ways you can create a Pandas Dataframe in Python. In this article, we will introduce you to a few methods that can be easily manipulated below. Follow along and choose the method that works for you.
Method 1: Create Pandas Dataframe with a list of lists
In this method you will use pre-made data in a variable called list. Then use the syntax to convert it to Pandas Dataframe. Following is the example:
You have the following data variable:
list = [[2,5],[3,6],[7,9],[5,6]]
Input:
import pandas as pd
df = pd.DataFrame(list)
print(df)
output
0 1
0 2 5
1 3 6
2 7 9
3 5 6
In Padas, data will be indexed from 0 onwards.
Method 2: Create Pandas Dataframe manually according to the syntax
With this method you would apply this syntax:
import pandas as pd
data = {'first_column': ['first_value', 'second_value', ...],
'second_column': ['first_value', 'second_value', ...],
....
}
df = pd.DataFrame(data)
print (df)
For example:
import pandas as pd
data = {'product': ['Cars', 'Flowers', 'Book', 'Cake'],
'price': [800, 10, 30, 80]
}
df = pd.DataFrame(data)
print (df)
Output
product price
0 Cars 800
1 Flowers 10
2 Book 30
3 Cake 80
Alternatively, if you want to set the index by a specific name, you can add the index parameters
Method 3: Create Pandas Dataframe by importing CSV file
The syntax for this method is as follows:
import pandas as pd
data = pd.read_csv(r'Path where the CSV file is stored\File name.csv')
df = pd.DataFrame(data)
print (df)
For example:
Here is the path to your file C:\Users\Kim\Desktop\product_price.csv
Your data:
product | price |
Cars | 800 |
Flowers | 10 |
Book | 30 |
Cake | 80 |
To create Pandas Dataframe, we declare the following code
import pandas as pd
data = pd.read_csv(r'C:\Users\Kim\Desktop\product_price.csv')
df = pd.DataFrame(data)
print (df)
Output:
product price
0 Cars 800
1 Flowers 10
2 Book 30
3 Cake 80
Method 4: Using Numpy syntax
To create pandas dataframe you can also use the syntax of numpy arrays. For example as follows:
import numpy as np
data_nparray = np.array([['John', 2018, '15'],
['Mike', 2015, '17'],
['Mark', 2018, '15'],
])
df = pd.DataFrame(data=data_nparray)
df
Output:
0 1 2
0 John 2018 15
1 Mike 2015 17
2 Mark 2018 15
Conclusion
Above, we have helped you to give the answer to the topic “Create Pandas DataFrame in Python”. If you have any further questions, you can leave your comments in the Comments section. I hope you have a productive day with your subject. See you in the next session!
Leave a comment