數據幀是數據的二維集合。這是一種數據結構,其中數據以表格形式存儲。數據集按行和列排列;我們可以在數據幀中存儲多個數據集。我們可以執行各種算術運算,例如在數據幀中添加列/行選擇和列/行。
我們可以從外部存儲器導入數據幀;這些存儲可以稱為 SQL 資料庫、CSV 文件和 Excel 文件。我們也可以使用列表、字典、以及來自列表的字典等。
在本教程中,我們將學習以多種方式創建數據幀。讓我們理解這些不同的方式。
首先,我們需要將Pandas庫安裝到 Python 環境中。
空數據幀
我們可以創建一個基本的空數據幀。需要調用 dataframe 構造器來創建 DataFrame。讓我們理解下面的例子。
示例-
# import pandas as pd
import pandas as pd
# Calling DataFrame constructor
df = pd.DataFrame()
print(df)
輸出:
Empty DataFrame
Columns: []
Index: []
方法 2:使用列表創建數據幀
我們可以使用單個列表或列表列表來創建數據幀。讓我們理解下面的例子。
示例-
# importing pandas library
import pandas as pd
# string values in the list
lst = ['Java', 'Python', 'C', 'C++',
'JavaScript', 'Swift', 'Go']
# Calling DataFrame constructor on list
dframe = pd.DataFrame(lst)
print(dframe)
輸出:
0 Java
1 Python
2 C
3 C++
4 JavaScript
5 Swift
6 Go
方法 3:從一組數據/列表中創建數據幀
數組/列表的字典可用於創建數據幀,所有的數組必須具有相同的長度。默認情況下,索引將是一個範圍(n);其中 n 表示陣列長度。讓我們理解下面的例子。
示例-
import pandas as pd
# assign data of lists.
data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
print(df)
輸出:
Name Age
0 Tom 20
1 Joseph 21
2 Krish 19
3 John 18
方法 4:使用數組創建索引數據幀
讓我們理解以下使用數組創建索引數據幀的示例。
示例-
# DataFrame using arrays.
import pandas as pd
# assign data of lists.
data = {'Name':['Renault', 'Duster', 'Maruti', 'Honda City'], 'Ratings':[9.0, 8.0, 5.0, 3.0]}
# Creates pandas DataFrame.
df = pd.DataFrame(data, index =['position1', 'position2', 'position3', 'position4'])
# print the data
print(df)
輸出:
Name Ratings
position1 Renault 9.0
position2 Duster 8.0
position3 Maruti 5.0
position4 Honda City 3.0
解釋-
在上面的代碼中,我們已經用各種汽車名稱及其等級定義了列名。我們使用數組來創建索引。
方法 5:從字典列表中創建數據幀
我們可以將字典列表作為輸入數據來創建 Pandas 數據幀。默認情況下,列名被視為鍵。讓我們理解下面的例子。
示例-
# the example is to create
# Pandas DataFrame by lists of dicts.
import pandas as pd
# assign values to lists.
data = [{'A': 10, 'B': 20, 'C':30}, {'x':100, 'y': 200, 'z': 300}]
# Creates DataFrame.
df = pd.DataFrame(data)
# Print the data
print(df)
輸出:
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
讓我們來理解另一個例子,用行索引和列索引從字典列表中創建 pandas dataframe。
示例- 2:
import pandas as pd
# assigns values to lists.
data = [{'x': 1, 'y': 2}, {'A': 15, 'B': 17, 'C': 19}]
# With two column indices, values same
# as dictionary keys
dframe1 = pd.DataFrame(data, index =['first', 'second'], columns =['x', 'y'])
# With two column indices with
# one index with other name
dframe2 = pd.DataFrame(data, index =['first', 'second'], columns =['x', 'y1'])
# print the first data frame
print (dframe1, "\n")
# Print the second DataFrame.
print (dframe2)
輸出:
x y
first 1.0 2.0
second NaN NaN
x y1
first 1.0 NaN
second NaN NaN
讓我們理解另一個通過傳遞字典和行的列表來創建數據幀的例子。
示例- 3
# The example is to create
# Pandas DataFrame by passing lists of
# Dictionaries and row indices.
import pandas as pd
# assign values to lists
data = [{'x': 2, 'z':3}, {'x': 10, 'y': 20, 'z': 30}]
# Creates padas DataFrame by passing
# Lists of dictionaries and row index.
dframe = pd.DataFrame(data, index =['first', 'second'])
# Print the dataframe
print(dframe)
輸出:
x y z
first 2 NaN 3
second 10 20.0 30
我們已經討論了使用字典列表創建數據幀的三種方法。
方法 6:使用 zip()函數創建數據幀
zip()函數用於合併兩個列表。讓我們理解下面的例子。
示例-
# The example is to create
# pandas dataframe from lists using zip.
import pandas as pd
# List1
Name = ['tom', 'krish', 'arun', 'juli']
# List2
Marks = [95, 63, 54, 47]
# two lists.
# and merge them by using zip().
list_tuples = list(zip(Name, Marks))
# Assign data to tuples.
print(list_tuples)
# Converting lists of tuples into
# pandas Dataframe.
dframe = pd.DataFrame(list_tuples, columns=['Name', 'Marks'])
# Print data.
print(dframe)
輸出:
[('john', 95), ('krish', 63), ('arun', 54), ('juli', 47)]
Name Marks
0 john 95
1 krish 63
2 arun 54
3 juli 47
方法 7:從系列字典創建數據幀
可以傳遞字典來創建數據幀。我們可以使用系列的 Dicts,其中後續的索引是傳遞的索引值的所有系列的並集。讓我們理解下面的例子。
示例-
# Pandas Dataframe from Dicts of series.
import pandas as pd
# Initialize data to Dicts of series.
d = {'Electronics' : pd.Series([97, 56, 87, 45], index =['John', 'Abhinay', 'Peter', 'Andrew']),
'Civil' : pd.Series([97, 88, 44, 96], index =['John', 'Abhinay', 'Peter', 'Andrew'])}
# creates Dataframe.
dframe = pd.DataFrame(d)
# print the data.
print(dframe)
輸出:
Electronics Civil
John 97 97
Abhinay 56 88
Peter 87 44
Andrew 45 96
在本教程中,我們討論了創建數據幀的不同方法。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/295953.html