Chuyển đổi danh sách mảng sang dataframe python

Chúng ta có thể chuyển đổi cột DataFrame của pandas thành một mảng Numpy bằng cách sử dụng các hàm 


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4 và 

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
5. Sử dụng hàm

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4, chúng ta có thể chuyển đổi toàn bộ DataFrame thành mảng NumPy. Pandas cung cấp nhiều chức năng khác nhau để thao tác hoặc phân tích dữ liệu của chúng tôi. Sử dụng một số chức năng này, chúng ta có thể dễ dàng chuyển đổi cấu trúc dữ liệu này sang cấu trúc dữ liệu khác

Trong bài viết này, tôi sẽ giải thích cách chuyển đổi cột DataFrame thành mảng Numpy bằng các hàm và thuộc tính khác nhau với các ví dụ

1. Ví dụ nhanh để chuyển đổi cột DataFrame thành mảng Numpy

Nếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách chuyển đổi cột DataFrame thành mảng NumPy


# Below are quick examples
# Example 1: Convert specific column use to_numpy[]
array = df['Courses'].to_numpy[]

# Example 2: Convert all columns to numpy array
array = df.to_numpy[]

# Example 3: Convert df column to array using df.Values 
array = df['Fee'].values

# Example 4: Convert Pandas column to array use slicing 
array = df[df.columns[3:]].to_numpy[]

# Example 5: Convert column to NumPy array use iloc[]
array = df.iloc[:,-1:].values

# Example 6: Convert column name to array
array = [df.columns.to_numpy[]]

Bây giờ, hãy tạo một DataFrame gấu trúc với một vài hàng và cột, thực hiện các ví dụ này và xác thực kết quả. Khung dữ liệu của chúng tôi chứa các tên cột 


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
1, 

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
2, 

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
3 và 

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]

Sản lượng dưới sản lượng

Khung dữ liệu gấu trúc

2. Chuyển đổi cột Pandas DataFrame thành NumPy Array

Chúng ta có thể chuyển đổi cột DataFrame của gấu trúc thành mảng NumPy bằng cách sử dụng hàm


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4. Hãy xem cách chuyển đổi các cột cụ thể [một hoặc nhiều] từ DataFrame sang mảng NumPy, trước tiên hãy chọn cột được chỉ định từ DataFrame bằng cách sử dụng ký hiệu ngoặc [] sau đó, gọi hàm

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4. Nó sẽ chuyển đổi một cột được chỉ định của Pandas DataFrame thành một mảng NumPy


# Convert specific column use to_numpy[]
array = df['Courses'].to_numpy[]
print[array]

# Convert specific columns 
array = df[['Courses', 'Duration']].to_numpy[]
print[array]

Năng suất dưới sản lượng

mảng NumPy

mảng NumPy

Hơn nữa, sử dụng hàm


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4, chúng ta có thể chuyển đổi toàn bộ DataFrame của gấu trúc thành một mảng NumPy. Nó trả về mảng NumPy hai chiều


# Convert all columns to numpy array
array = df.to_numpy[]
print[array]

# Output
# [['Spark' 20000 '30days' 1000]
# ['PySpark' 25000 '40days' 2300]
# ['Python' 22000 '35days' 1200]
# ['pandas' 30000 '50days' 2000]]

3. Chuyển đổi Cột Pandas thành Mảng bằng cách sử dụng Giá trị []

Trong phần này, chúng tôi sẽ chuyển đổi cột DataFrame của gấu trúc thành một mảng NumPy bằng cách sử dụng


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
8. Hàm values[] trả về biểu diễn mảng NumPy của DataFrame. Do đó, trục hàng và cột [nhãn] không xuất hiện. Ví dụ,


# Convert df column to array using df.Values 
array = df['Fee'].values
print[array]

# Output:
# [20000 25000 22000 30000]

4. Sử dụng Pandas Slicing với to_numpy[] & Convert Array

Như chúng ta biết rằng bằng cách sử dụng Pandas slicer, chúng ta có thể chọn một phần cụ thể của các hàng hoặc cột của một DataFrame nhất định. Ở đây, tôi sẽ chọn một phần cụ thể của cột mà chúng tôi muốn chuyển đổi thành một mảng NumPy và gọi hàm


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4. Nó sẽ chuyển đổi một cột hoặc một phần cụ thể của cột thành một mảng NumPy


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
0

5. Sử dụng thuộc tính Pandas iloc[] để chuyển đổi mảng

Ngoài ra, bằng cách sử dụng thuộc tính Pandas iloc[], chúng ta có thể chọn một cột được chỉ định và sau đó gọi các giá trị. Cú pháp này sẽ chuyển đổi một cột DataFrame được chỉ định thành một mảng NumPy


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
1

6. Chuyển đổi tên cột thành mảng NumPy

Sử dụng hàm


# Convert specific column use to_numpy[]
array = df['Courses'].to_numpy[]
print[array]

# Convert specific columns 
array = df[['Courses', 'Duration']].to_numpy[]
print[array]
0 cùng với hàm

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4, chúng ta có thể chuyển đổi tên cột của Pandas DataFrame thành một mảng NumPy. Hãy áp dụng cú pháp dưới đây và chuyển đổi nó thành một mảng NumPy


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
3

7. Phần kết luận

Trong bài viết này, chúng ta đã học cách chuyển đổi cột Pandas DataFrame thành mảng NumPy bằng cách sử dụng các hàm 


import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
4 và 

import pandas as pd
import numpy as np
technologies = {
    'Courses':["Spark","PySpark","Python","pandas"],
    'Fee' :[20000,25000,22000,30000],
    'Duration':['30days','40days','35days','50days'],
    'Discount':[1000,2300,1200,2000]
              }
index_labels=['r1','r2','r3','r4']
df = pd.DataFrame[technologies,index=index_labels]
print[df]
5 và các thuộc tính khác nhau. Ngoài ra, đã học cách chuyển đổi tên cột thành một mảng

Làm cách nào để chuyển đổi danh sách các danh sách thành DataFrame trong Python?

Tóm tắt. Để chuyển đổi danh sách các danh sách thành Khung dữ liệu Pandas, hãy sử dụng pd. Trình xây dựng DataFrame[] và chuyển danh sách các danh sách làm đối số . Đối số cột tùy chọn có thể giúp bạn cấu trúc đầu ra.

Bạn có thể tạo DataFrame từ danh sách trong Python không?

Danh sách có thể có các phần tử không đồng nhất, i. e. , nó có thể có các giá trị thuộc các loại khác nhau. Để phân tích một Danh sách như vậy, chúng ta có thể chuyển đổi nó thành DataFrame của gấu trúc. Bằng cách chuyển đổi Danh sách thành cấu trúc 2 chiều giúp xử lý hiệu quả. DataFrame có thể được tạo từ Danh sách bằng cách sử dụng hàm tạo DataFrame .

Tôi có thể lưu trữ danh sách trong DataFrame Python không?

Bạn có thể chèn danh sách các giá trị vào một ô trong Pandas DataFrame bằng DataFrame. tại[] , Khung dữ liệu. iat[] và DataFrame. các phương thức loc[] .

Chủ Đề