代码示例:
import pandas as pd
df1 = pd.read_csv('test.csv')
print(df1)
'''
打印:
userId score age
0 1 45 18
1 2 65 19
2 3 58 17
3 4 92 16
4 5 78 18
'''
print(df1.shape) #获取行和列,打印(5, 3)
print(df1.index.to_list()) #获取行索引,打印[0, 1, 2, 3, 4]
print(df1.columns.to_list()) #获取列索引,打印['userId', 'score', 'age']
print(df1.ndim) #获取数据的维度,打印2
print(df1.values)
'''
values属性也会以二维ndarray的形式返回DataFrame的数据
打印:
[[ 1 45 18]
[ 2 65 19]
[ 3 58 17]
[ 4 92 16]
[ 5 78 18]]
'''
print(df1.info())
'''
展示df的概览信息
打印:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
userId 5 non-null int64
score 5 non-null int64
age 5 non-null int64
dtypes: int64(3)
memory usage: 200.0 bytes
None
'''
print(df1.head(3))
'''
展示头几行,默认显示5行
打印:
userId score age
0 1 45 18
1 2 65 19
2 3 58 17
'''
print(df1.tail(2))
'''
展示尾几行,默认显示5行
打印:
userId score age
3 4 92 16
4 5 78 18
'''