DataFrame
Sometimes we need to work on multiple columns at a time, i.e., we need to process the tabular data. For example, the result of a class, items in a restaurant’s menu, reservation chart of a train, etc. Pandas store such tabular data using a DataFrame. A DataFrame is a two-dimensional labelled data structure like a table of MySQL. It contains rows and columns, and therefore has both a row and column index. Each column can have a different type of value such as numeric, string, boolean, etc., as in tables of a database.
Creation of DataFrame There are a number of ways to create a DataFrame. Some of them are listed in this section. (A) Creation of an empty DataFrame An empty DataFrame can be created as follows:
import pandas as pd
dFrameEmt = pd.DataFrame()
print(dFrameEmt)
Creation of DataFrame from NumPy ndarrays
Consider the following three NumPy ndarrays. Let us create a simple DataFrame without any column labels, using a single ndarray:
We can create a DataFrame using more than one ndarrays, as shown in the following example:
import numpy as np
import pandas as pd
array1 = np.array([10,20,30])
array2 = np.array([100,200,300])
array3 = np.array([-10,-20,-30, -40])
dFrame1 = pd.DataFrame([array1,array1,array1])
print("DataFrame1",dFrame1)
Creation of DataFrame from List of Dictionaries
We can create DataFrame from a list of Dictionaries, for example:
# Create list of dictionaries
import numpy as np
import pandas as pd
listDict = [{'a':10, 'b':20}, {'a':5,'b':10, 'c':20}]
dFrameListDict = pd.DataFrame(listDict)
print(dFrameListDict)
In above example Here, the dictionary keys are taken as column labels, and the values corresponding to each key are taken as rows. There will be as many rows as the number of dictionaries present in the list. In the above example there are two dictionaries in the list. So, the DataFrame consists of two rows. Number of columns in a DataFrame is equal to the maximum number of keys in any dictionary of the list. Hence, there are three columns as the second dictionary has three elements. Also, note that NaN (Not a Number) is inserted if a corresponding value for a column is missing.
Click Here for Part 2 For Defining of DataFrame
0 Comments