在Python中,查看数据集行数的方法取决于数据集的类型。以下是两种常见情况的方法:
使用NumPy数组:
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rows, cols = data.shape
print("行数:", rows)
使用Pandas DataFrame:
import pandas as pd
df = pd.DataFrame({
'Country': ['China', 'China', 'India', 'India', 'America', 'Japan', 'China', 'India'],
'Income': [10000, 10000, 5000, 5002, 40000, 50000, 8000, 5000],
'Number': [5000, 4321, 1234, 4010, 250, 250, 4500, 4321]
})
方法一:使用 df.shape 获取行数和列数
print("行数:", df.shape)
方法二:使用 df.info() 获取数据概览
以上是两种查看数据集行数的方法。如果您使用的是其他类型的数据集,请根据具体情况选择合适的方法。