Từ điển là một cấu trúc dữ liệu tích hợp sẵn của Python, bao gồm các cặp khóa-giá trị. Trong thời gian ngắn này… ![Làm cách nào để chuyển đổi từ điển của từ điển sang DataFrame trong python?](/dist/images/loading.svg) Đội Aporia Cách thực hiện Làm cách nào để xóa hàng dựa trên giá trị cột trong DataFrame?Một hàng trong DataFrame có thể được coi là một quan sát với một số tính năng được biểu diễn… Đội Aporia Cách thực hiện Làm cách nào để chuyển đổi chỉ mục của Khung dữ liệu thành Cột?DataFrame là cấu trúc dữ liệu hai chiều với các hàng và cột được gắn nhãn. Nhãn hàng còn được gọi là…
Bạn có thể sử dụng một trong các phương pháp sau để chuyển đổi từ điển trong Python thành DataFrame của gấu trúc Phương pháp 1. sử dụng chính tả. mặt hàng() df = pd.DataFrame(list(some_dict.items()), columns = ['col1', 'col2'])
Phương pháp 2. Sử dụng from_dict() df = pd.DataFrame.from_dict(some_dict, orient='index').reset_index()
df.columns = ['col1', 'col2'] Cả hai phương pháp tạo ra cùng một kết quả Các ví dụ sau đây cho thấy cách sử dụng từng phương pháp trong thực tế ví dụ 1. Chuyển đổi từ điển sang DataFrame bằng dict. mặt hàng()Giả sử chúng ta có từ điển sau bằng Python #create dictionary
some_dict = {'Lebron':26,'Luka':30,'Steph':22,'Nicola':29, 'Giannis':31}
Chúng ta có thể sử dụng đoạn mã sau để chuyển đổi từ điển này thành DataFrame của gấu trúc import pandas as pd
#convert dictionary to pandas DataFrame
df = pd.DataFrame(list(some_dict.items()), columns = ['Player', 'Points'])
#view DataFrame
df
Player Points
0 Lebron 26
1 Luka 30
2 Steph 22
3 Nicola 29
4 Giannis 31
Chúng ta cũng có thể sử dụng hàm type() để xác nhận rằng kết quả là DataFrame của gấu trúc #display type of df
type(df)
pandas.core.frame.DataFrame
ví dụ 2. Chuyển đổi từ điển sang DataFrame bằng from_dict()Giả sử chúng ta có từ điển sau bằng Python #create dictionary
some_dict = {'Lebron':26,'Luka':30,'Steph':22,'Nicola':29, 'Giannis':31}
Chúng ta có thể sử dụng đoạn mã sau để chuyển đổi từ điển này thành DataFrame của gấu trúc import pandas as pd
#convert dictionary to pandas DataFrame
df = pd.DataFrame.from_dict(some_dict, orient='index').reset_index()
#define column names of DataFrame
df.columns = ['Player', 'Points']
#view DataFrame
df
Player Points
0 Lebron 26
1 Luka 30
2 Steph 22
3 Nicola 29
4 Giannis 31
Chúng ta cũng có thể sử dụng hàm type() để xác nhận rằng kết quả là DataFrame của gấu trúc #display type of df
type(df)
pandas.core.frame.DataFrame Lưu ý rằng phương pháp này tạo ra kết quả chính xác giống như phương pháp trước đó Tài nguyên bổ sungCác hướng dẫn sau đây giải thích cách thực hiện các tác vụ phổ biến khác trong gấu trúc Cách chuyển đổi khung dữ liệu Pandas sang từ điển Cách chuyển đổi bảng Pandas Pivot sang DataFrame Cách chuyển đổi đầu ra Pandas GroupBy thành DataFrame Sử dụng các phương thức from_dict(), from_records(), json_normalize() để chuyển đổi danh sách từ điển (dict) sang DataFrame của gấu trúc. Dict là một loại trong python để giữ các cặp khóa-giá trị. Khóa được sử dụng làm tên cột và giá trị được sử dụng cho giá trị cột khi chúng tôi chuyển đổi dict thành DataFrame. Khi một khóa không được tìm thấy đối với một số ký tự và nó tồn tại trên các ký tự khác, nó sẽ tạo một DataFrame với
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
1 cho các khóa không tồn tạiTrong bài viết này, chúng ta sẽ xem cách chuyển đổi danh sách từ điển (dict) thành DataFrame của gấu trúc bằng cách sử dụng
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
2,
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
3 và
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
4. Mỗi từ điển trong danh sách có thể có các khóa giống nhau hoặc khác nhau nhưng giá trị khác nhau1. Ví dụ nhanh về chuyển đổi danh sách từ điển thành khung dữ liệuNếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách chuyển đổi danh sách từ điển (dict) thành Pandas DataFrame
# Below are quick example
# Create a list of dictionary objects
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Convert a List of dictionaries using from_records() method.
df = pd.DataFrame.from_records(technologies)
# Set Custom index by using index parameter.
df = pd.DataFrame.from_records(technologies, index=['1', '2', '3'])
# Convert a List of Dictionaries by from_dict method.
df = pd.DataFrame.from_dict(data)
# Dictionary orientations of column.
df=pd.DataFrame.from_dict(technologies, orient='columns')
# Convert a list of dictionaries using json_normalize().
df=pd.json_normalize(technologies)
1. Tạo một DataFrame từ Danh sách DictNếu bạn có một danh sách từ điển (dict), bạn có thể dễ dàng tạo DataFrame bằng cách sử dụng hàm tạo DataFrame. Để biết thêm ví dụ, hãy tham khảo cách tạo DataFrame gấu trúc với các ví dụ ________số 8_______Sản lượng dưới sản lượng. Lưu ý rằng khi một khóa không được tìm thấy đối với một số ký tự và nó tồn tại trên các ký tự khác, nó sẽ tạo một DataFrame với
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
1 cho các khóa không tồn tại. Trong trường hợp bạn muốn thay đổi các giá trị NaN, hãy tham khảo Cách thay thế các giá trị NaN/Không có chuỗi rỗng
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
2. Sử dụng phương thức from_records() Chuyển đổi danh sách từ điểnPhương thức
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
6 được sử dụng để chuyển đổi danh sách từ điển sang DataFrame. Nó cũng có thể được sử dụng để chuyển đổi cấu trúc hoặc bản ghi
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
7 thành DataFrame và được sử dụng để tạo DataFrame từ ndarray có cấu trúc, chuỗi của
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
8 hoặc
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
9 hoặc từ một DataFrame khác
# Convert a List of dictionaries using from_records() method.
df = pd.DataFrame.from_records(technologies)
print(df)
Mang lại sản lượng tương tự như trên 3. Đặt chỉ mục tùy chỉnh bằng cách sử dụng tham số chỉ mụcTrong phần này, hãy xem cách đặt chỉ mục tùy chỉnh bằng cách sử dụng tham số
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
0. Nếu bạn đã có DataFrame, bạn có thể đặt Chỉ mục cho DataFrame bằng cách sử dụng
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
1
# Set Custom index by using index parameter.
df = pd.DataFrame.from_records(technologies,index=['1', '2', '3'])
print(df)
Sản lượng dưới sản lượng
Courses Duration Discount Fee Courses_Fee Duration_Discount
1 Spark 30days 1000.0 NaN NaN NaN
2 python NaN NaN 25000.0 Spark NaN
3 NaN 35days NaN 30000.0 NaN 10days
4. Chuyển đổi danh sách từ điển bằng cách sử dụng phương thức from_dict()Sử dụng
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
3 để chuyển đổi danh sách từ điển thành DataFrame của gấu trúc. Hàm này được sử dụng để xây dựng DataFrame từ dict của mảng giống như hoặc dicts
# Convert a List of Dictionaries by from_dict method.
df = pd.DataFrame.from_dict(technologies)
print(df)
Sản lượng dưới sản lượng
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
5. Định hướng từ điển của cột và chỉ mụcCó hai loại định hướng từ điển chính được gọi là
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
3 và
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
0. Đó là phân biệt giữa các loại định hướng từ điển khác nhau với
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
5
# Dictionary orientations of column.
df=pd.DataFrame.from_dict(technologies, orient='columns')
print(df)
Mang lại sản lượng tương tự như trên 6. Chuyển đổi danh sách từ điển bằng cách sử dụng json_normalize()Nếu chúng ta muốn chuyển đổi một đối tượng thành một chuỗi
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
6, chúng ta phải lưu ý rằng_______15_______7 và
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
8 sẽ được chuyển đổi thành null và các đối tượng ngày giờ sẽ được chuyển đổi thành dấu thời gian
Courses Duration Discount Fee Courses_Fee Duration_Discount
0 Spark 30days 1000.0 NaN NaN NaN
1 python NaN NaN 25000.0 Spark NaN
2 NaN 35days NaN 30000.0 NaN 10days
9. Hàm
# Convert a List of dictionaries using from_records() method.
df = pd.DataFrame.from_records(technologies)
print(df)
0 hoạt động với danh sách từ điển (dict)
# Convert a list of dictionaries using json_normalize.
df=pd.json_normalize(technologies)
print(df)
Mang lại sản lượng tương tự như trên 7. Hoàn thành các ví dụ để chuyển đổi danh sách từ điển của Pandas DataFrame
# Create a DataFrame from list of Dictionaries with default indexes.
import pandas as pd
# List of dict object
technologies=[{'Courses':'Spark','Duration':'30days','Discount':1000}
{'Courses':'python','Fee':25000,'Courses_Fee':'Spark'},
{'Fee':30000,'Duration':'35days','Duration_Discount':'10days'}]
# Create DataFrame from list of dic object
df=pd.DataFrame(technologies)
print(df)
0Phần kết luậnTrong bài viết này, bạn đã tìm hiểu về cách chuyển đổi danh sách từ điển sang DataFrame của pandas bằng from_record(), from_dict(), json_normalize() với các ví dụ
Làm cách nào để chuyển đổi từ điển thành trăn DataFrame?
Chúng tôi có thể chuyển đổi từ điển thành khung dữ liệu gấu trúc bằng cách sử dụng pd. Khung dữ liệu. phương thức lớp from_dict() .
Làm cách nào để chuyển đổi danh sách từ điển thành trăn DataFrame?
Sử dụng các phương thức from_dict(), from_records(), json_normalize() để chuyển đổi danh sách từ điển (dict) sang DataFrame của gấu trúc . Dict là một loại trong python để giữ các cặp khóa-giá trị. Khóa được sử dụng làm tên cột và giá trị được sử dụng cho giá trị cột khi chúng tôi chuyển đổi dict thành DataFrame.
Làm cách nào để chuyển đổi danh sách từ điển thành DataFrame?
Phương pháp 1. Chuyển đổi danh sách từ điển thành DataFrame gấu trúc bằng cách sử dụng from_records . Pandas hàm from records() của DataFrame. Nó thay đổi dữ liệu có cấu trúc hoặc bản ghi thành DataFrames. Nó chuyển đổi một chuỗi ndarray, tuple hoặc dict có cấu trúc hoặc DataFrame thành một đối tượng DataFrame.
Từ điển từ điển có thể được sử dụng để tạo DataFrame của gấu trúc không?
Chúng ta có thể tạo một khung dữ liệu bằng Pandas. Phương thức DataFrame() . Thí dụ. Tạo pandas Dataframe từ từ điển của từ điển. |