📜  如何从 Pandas DataFrame 中获取单元格值?

📅  最后修改于: 2022-05-13 01:54:57.604000             🧑  作者: Mango

如何从 Pandas DataFrame 中获取单元格值?

在本文中,我们将讨论如何从 pandas 数据框中获取单元格值。

方法一:使用 loc()函数

此方法用于通过使用列名获取具有索引函数的特定单元格数据

语法

在哪里,

  • 数据框是输入数据框
  • index是获取单元格row_numer的函数
  • column_name 表示单元格列名

示例:使用 loc()函数获取特定单元格的Python程序

Python3
# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using loc() function
# in id column and 1 row
print(data['id'].loc[data.index[0]])
 
# get the cell value using loc() function
# in id column and 2 row
print(data['id'].loc[data.index[1]])
 
# get the cell value using loc() function
# in id column and 3 row
print(data['id'].loc[data.index[2]])
 
# get the cell value using loc() function
# in id column and 4 row
print(data['id'].loc[data.index[3]])
 
# get the cell value using loc() function
# in name column and 1 row
print(data['name'].loc[data.index[0]])
 
# get the cell value using loc() function
# in name column and 2 row
print(data['name'].loc[data.index[1]])
 
# get the cell value using loc() function
# in subjects column and 3 row
print(data['subjects'].loc[data.index[2]])
 
# get the cell value using loc() function
# in subjects column and 4 row
print(data['subjects'].loc[data.index[3]])


Python3
# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using iloc() function
# in id column and 1 row
print(data['id'].iloc[0])
 
# get the cell value using iloc() function
# in id column and 2 row
print(data['id'].iloc[1])
 
# get the cell value using iloc() function
# in name column and 1 row
print(data['name'].iloc[0])
 
# get the cell value using iloc() function
# in id column and 4 row
print(data['name'].iloc[3])
 
# get the cell value using iloc() function
# in subjects column and 1 row
print(data['subjects'].iloc[0])
 
# get the cell value using iloc() function
# in subjects column and 4 row
print(data['subjects'].iloc[3])


Python3
# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using values() function
# in id column and 1 row
print(data['name'].values[0])
 
# get the cell value using values() function
# in id column and 2 row
print(data['id'].values[1])
 
# get the cell value using values() function
# in name column and 1 row
print(data['name'].values[0])
 
# get the cell value using values() function
# in id column and 4 row
print(data['name'].values[3])
 
# get the cell value using values() function
# in subjects column and 1 row
print(data['subjects'].values[0])
 
# get the cell value using values() function
# in subjects column and 4 row
print(data['subjects'].values[3])


Python3
# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using at() function
# in id column and 1 row
print(data.at[0, 'id'])
 
# get the cell value using at() function
# in id column and 2 row
print(data.at[1, 'id'])
 
# get the cell value using at() function
# in name column and 1 row
print(data.at[0, 'name'])
 
# get the cell value using at() function
# in id column and 4 row
print(data.at[3, 'name'])
 
# get the cell value using at() function
# in subjects column and 1 row
print(data.at[0, 'subjects'])
 
# get the cell value using values() function
# in subjects column and 4 row
print(data.at[3, 'subjects'])


Python3
# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using iat() function
# in id column and 1 row
print(data.iat[0, 0])
 
# get the cell value using iat() function
# in name column and 1 row
print(data.iat[0, 1])
 
# get the cell value using iat() function
# in id column and 2 row
print(data.iat[1, 0])


输出:

7058
7059
7072
7054
sravan
jyothika
html/php
php/js

方法二:使用 iloc[]

此方法用于通过使用列名和行索引号来访问单元格数据。

语法

在哪里

  • 数据框是输入数据框
  • row_numer 表示单元格的单元格行位置
  • column_name 表示单元格列名

示例:使用 iloc函数获取单元格值的Python程序

Python3

# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using iloc() function
# in id column and 1 row
print(data['id'].iloc[0])
 
# get the cell value using iloc() function
# in id column and 2 row
print(data['id'].iloc[1])
 
# get the cell value using iloc() function
# in name column and 1 row
print(data['name'].iloc[0])
 
# get the cell value using iloc() function
# in id column and 4 row
print(data['name'].iloc[3])
 
# get the cell value using iloc() function
# in subjects column and 1 row
print(data['subjects'].iloc[0])
 
# get the cell value using iloc() function
# in subjects column and 4 row
print(data['subjects'].iloc[3])

输出:

7058
7059
sravan
ramya
java
php/js

方法 3:使用 values()函数

这将通过提供 row_number 和 column_name 返回单元格值。

语法

在哪里,

  • 数据框是输入数据框
  • row_numer 表示单元格的单元格行位置
  • column_name 表示单元格列名

示例:从数据框中获取单元格值的Python代码

Python3

# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using values() function
# in id column and 1 row
print(data['name'].values[0])
 
# get the cell value using values() function
# in id column and 2 row
print(data['id'].values[1])
 
# get the cell value using values() function
# in name column and 1 row
print(data['name'].values[0])
 
# get the cell value using values() function
# in id column and 4 row
print(data['name'].values[3])
 
# get the cell value using values() function
# in subjects column and 1 row
print(data['subjects'].values[0])
 
# get the cell value using values() function
# in subjects column and 4 row
print(data['subjects'].values[3])

输出:

sravan
7059
sravan
ramya
java
php/js

方法四:使用 at[]函数

此函数将通过获取行号和列名返回单元格值。

语法

在哪里

  • 数据框是输入数据框
  • row_index 根据给定的索引号获取行
  • column_name 是列的名称

示例:使用 at[]函数获取单元格值的Python程序

Python3

# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using at() function
# in id column and 1 row
print(data.at[0, 'id'])
 
# get the cell value using at() function
# in id column and 2 row
print(data.at[1, 'id'])
 
# get the cell value using at() function
# in name column and 1 row
print(data.at[0, 'name'])
 
# get the cell value using at() function
# in id column and 4 row
print(data.at[3, 'name'])
 
# get the cell value using at() function
# in subjects column and 1 row
print(data.at[0, 'subjects'])
 
# get the cell value using values() function
# in subjects column and 4 row
print(data.at[3, 'subjects'])

输出:

7058
7059
sravan
ramya
java
php/js

方法 5:使用 iat[]函数

此函数采用行和列索引来显示数据框中的单元格值。

句法:

在哪里,

  • 数据框是输入数据框
  • row_index 是行号
  • column_index 是列号

示例:使用 iat函数获取单元格值的Python程序

Python3

# import pandas module
import pandas as pd
 
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
 
# get the cell value using iat() function
# in id column and 1 row
print(data.iat[0, 0])
 
# get the cell value using iat() function
# in name column and 1 row
print(data.iat[0, 1])
 
# get the cell value using iat() function
# in id column and 2 row
print(data.iat[1, 0])

输出:

7058
sravan
7059