Cách lấy dòng và cột trong excel bằng python

Bạn có thể sử dụng các thuộc tính DataFrame


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
0,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
1,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
2,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
3 và các cách khác để lấy/chọn một giá trị ô từ Pandas DataFrame. Pandas DataFrame được cấu trúc dưới dạng các hàng & cột giống như một bảng và một ô được gọi là một khối cơ bản lưu trữ dữ liệu. Mỗi ô chứa thông tin liên quan đến sự kết hợp của hàng và cột

loc[] & iloc[] cũng được sử dụng để chọn hàng từ pandas DataFrame và chọn cột từ pandas DataFrame

1. Ví dụ nhanh về Nhận giá trị ô của DataFrame

Nếu bạn đang vội, dưới đây là một số ví dụ nhanh về cách chọn giá trị ô từ DataFrame của gấu trúc


# Belwo are quick example 
# Using loc[]. Get cell value by name & index
print[df.loc['r4']['Duration']]
print[df.loc['r4'][2]]

# Using iloc[]. Get cell value by index & name
print[df.iloc[3]['Duration']]
print[df.iloc[3,2]]


# Using DataFrame.at[]
print[df.at['r4','Duration']]
print[df.at[df.index[3],'Duration']]

# Using DataFrame.iat[]
print[df.iat[3,2]]

#Get a cell value
print[df["Duration"].values[3]]

# Get cell value from last row
print[df.iloc[-1,2]]
print[df.iloc[-1]['Duration']]
print[df.at[df.index[-1],'Duration']]

Bây giờ, hãy tạo một DataFrame với một vài hàng và cột và thực hiện một số ví dụ 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
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
4,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
5,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
6,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
7


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]

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


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
0

1. Sử dụng

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
8 để lấy giá trị ô theo tên cột

Trong Pandas, thuộc tính


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
8 được sử dụng để lấy giá trị ô cụ thể theo tên hàng & nhãn [tên cột]. Dưới đây tất cả các ví dụ trả về một giá trị ô từ nhãn hàng

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
00 và cột

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
6 [cột thứ 3]


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
5

Sản lượng dưới sản lượng. Từ các ví dụ trên,


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
02 trả về Sê-ri gấu trúc


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
7

2. Sử dụng

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
03 để lấy giá trị ô theo vị trí cột

Nếu bạn muốn lấy giá trị ô theo số cột hoặc vị trí chỉ mục, hãy sử dụng


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
03, vị trí chỉ mục bắt đầu từ 0 đến độ dài 1 [chỉ mục bắt đầu từ 0]. Để chỉ cột cuối cùng, hãy sử dụng -1 làm vị trí cột


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
0

Điều này trả về cùng một đầu ra như trên. Lưu ý rằng thuộc tính


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
1 không hỗ trợ

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
06, bằng cách sử dụng ký hiệu này, sẽ trả về lỗi

3. Sử dụng

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
07 để chọn Giá trị ô cụ thể theo Tên nhãn cột

Thuộc tính


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
07 được sử dụng để truy cập một ô duy nhất theo cặp nhãn hàng và cột. Giống như loc[] cái này không hỗ trợ cột theo vị trí. Điều này hoạt động tốt hơn khi bạn muốn lấy một giá trị ô cụ thể từ Pandas DataFrame vì nó sử dụng cả nhãn hàng và cột. Lưu ý rằng thuộc tính

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
2 không hỗ trợ chỉ mục phủ định để tham chiếu các hàng hoặc cột từ cuối cùng


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
6

Những ví dụ này cũng mang lại kết quả tương tự


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
50

4. Sử dụng

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
51 chọn Giá trị ô cụ thể theo Vị trí cột


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
51 là một thuộc tính khác để chọn một giá trị ô cụ thể theo vị trí hàng và cột. Sử dụng điều này, bạn chỉ có thể tham khảo cột theo vị trí chứ không phải theo nhãn. Điều này cũng không hỗ trợ vị trí cột hoặc chỉ mục phủ định


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
0

5. Chọn Giá trị Ô từ DataFrame Sử dụng df[‘col_name’]. giá trị[]

Chúng ta có thể sử dụng


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
53 để lấy DataFrame 1×1 dưới dạng mảng NumPy, sau đó truy cập giá trị đầu tiên và duy nhất của mảng đó để lấy giá trị ô, ví dụ:

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
54


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
3

6. Nhận giá trị ô từ hàng cuối cùng của Pandas DataFrame

Nếu bạn muốn lấy một giá trị ô cụ thể từ Hàng cuối cùng của Khung dữ liệu Pandas, hãy sử dụng chỉ mục phủ định để chỉ các hàng từ cuối cùng. Ví dụ: Chỉ mục -1 đại diện cho hàng cuối cùng và -2 cho hàng thứ hai từ hàng cuối cùng. Tương tự, bạn cũng nên sử dụng -1 cho cột cuối cùng


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
4

Để chọn giá trị ô của hàng cuối cùng và cột cuối cùng, hãy sử dụng


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
55, điều này trả về

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
56. Tương tự, bạn cũng có thể thử các cách tiếp cận khác

Phần kết luận

Trong bài viết này, bạn đã học cách lấy hoặc chọn một giá trị ô cụ thể từ pandas DataFrame bằng cách sử dụng các thuộc tính


import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
57,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
58,

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
59 &

import pandas as pd
technologies = {
     'Courses':["Spark","PySpark","Hadoop","Python","pandas"],
     'Fee' :[24000,25000,25000,24000,24000],
     'Duration':['30day','50days','55days', '40days','60days'],
     'Discount':[1000,2300,1000,1200,2500]
          }
index_labels=['r1','r2','r3','r4','r5']
df = pd.DataFrame[technologies, index=index_labels]
print[df]
70. Ngoài ra, bạn đã học cách lấy một giá trị cụ thể từ hàng cuối cùng và hàng cuối cùng, cột cuối cùng với các ví dụ

Chủ Đề