import pandas as pd # 创建示例 DataFrame data = { 'A': [1, 2, 3, 4, 5], 'B': ['a', 'b', 'c', 'd', 'e'], 'C': [10, 20, 30, 40, 50] } df = pd.DataFrame(data, index=['row1', 'row2', 'row3', 'row4', 'row5']) # 使用 loc 选择单个单元格 cell_value = df.loc['row2', 'B'] print("Cell Value:", cell_value) # 使用 loc 选择单行 row_data = df.loc['row3'] print("\nRow Data:\n", row_data) # 使用 loc 选择多行和多列 subset = df.loc[['row1', 'row4'], ['A', 'C']] print("\nSubset:\n", subset)
标签:loc,df,DataFrame,选择,print,data From: https://www.cnblogs.com/nicole-zhang/p/17613783.html