📜  如何在 DataFrame 中获取列名和行名?

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

如何在 DataFrame 中获取列名和行名?

在分析通常非常庞大的真实数据集时,我们可能需要获取行或索引名称和列名称才能执行某些操作。

注意:要下载以下示例中使用的nba数据集,请单击此处

在 Pandas 数据框中获取行名

首先,让我们用 nba.csv 创建一个简单的数据框

Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
# calling head() method 
# storing in new variable
data_top = data.head(10)
   
# display
data_top


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
# iterating the columns
for row in data_top.index:
    print(row, end = " ")


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
# list(data_top) or
list(data_top.index)


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
list(data_top.index.values)


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
list(data_top.index.values.tolist())


Python3
# iterate the indices and print each one
for row in data.index:
    print(row, end = " ")


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
row_count = 0
 
# iterating over indices
for col in data.index:
    row_count += 1
 
# print the row count
print(row_count)


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# iterating the columns
for col in data.columns:
    print(col)


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
# list(data) or
list(data.columns)


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
list(data.columns.values)


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
list(data.columns.values.tolist())


Python3
# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
# using sorted() method
sorted(data)


输出:

现在让我们尝试从上面的数据集中获取行名。

方法#1:简单地迭代索引

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
# iterating the columns
for row in data_top.index:
    print(row, end = " ")

输出:

0 1 2 3 4 5 6 7 8 9 


方法#2:使用带有数据框对象的行

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
# list(data_top) or
list(data_top.index)

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


方法#3: index.values 方法返回一个索引数组。

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
list(data_top.index.values)

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


方法#4:使用给定索引列表的值的 tolist() 方法。

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# calling head() method 
# storing in new variable
data_top = data.head()
   
list(data_top.index.values.tolist())

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

方法#5:计算数据框中的行数
由于我们使用 head() 方法仅加载了数据帧的前 10 行,因此让我们首先验证总行数。

Python3

# iterate the indices and print each one
for row in data.index:
    print(row, end = " ")

输出:

现在,让我们打印索引的总数。

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
row_count = 0
 
# iterating over indices
for col in data.index:
    row_count += 1
 
# print the row count
print(row_count)

输出:

458

在 Pandas 数据框中获取列名

现在让我们尝试从 nba.csv 数据集中获取列名。
方法#1:简单地遍历列

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
 
# iterating the columns
for col in data.columns:
    print(col)

输出:


方法#2:使用带有数据框对象的列

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
# list(data) or
list(data.columns)

输出:


方法#3: column.values 方法返回一个索引数组。

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
list(data.columns.values)

输出:


方法 #4:使用 tolist() 方法和给定列列表的值。

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
list(data.columns.values.tolist())

输出:


方法 #5:使用 sorted() 方法
Sorted() 方法将返回按字母顺序排序的列列表。

Python3

# Import pandas package
import pandas as pd
   
# making data frame
data = pd.read_csv("nba.csv")
   
# using sorted() method
sorted(data)

输出: