importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv') #hotelreviews50_1.csv文件与.py文件在同一级目录下
print(df.head(3)) #读取前3行
控制台输出:
从输出可见,默认会将第一行当成列名
(2)在读数之后自定义标题
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv') #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题
columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
df.columns=columns_nameprint(df.head(3)) #读取前3行
控制台输出:
(3)csv文件没有列标题,从第一行就直接开始是数据的录入了
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv',header=None) #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题
columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
df.columns=columns_nameprint(df.head(3)) #读取前3行
这个时候一定要加'header=None', 这样读进来的列名就是系统默认的0,1,2... 序列号
由上可见编号为201 的数据读进来了。
(4)使用pandas读取csv文件的指定列方法:pd.read_csv("filepath",usecols=[0,1,2,...],[encoding='编码'])
读取csv文件中每行的前3列
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv',header=None,usecols=[0,1,2,3]) #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题#columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
columns_name=['mysql_id','hotelname','customername','reviewtime']
df.columns=columns_nameprint(df.head(3)) #读取前3行
控制台输出:
(5)pandas 在Dataframe中新添加一列
其实在Dataframe中新添加一列很简单,直接指明列名,然后赋值就可以了。
df['split_word_result']='new'
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv',header=None) #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题
columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
df.columns=columns_name
df['split_word_result']='new'
print(df.head(3))
控制台输出:
3.Pandas查看数据表信息
(1)维度查看
利用df.shape
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv',header=None,usecols=[0,1,2,3]) #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题#columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
columns_name=['mysql_id','hotelname','customername','reviewtime']
df.columns=columns_nameprint(df.head(3)) #读取前3行
print(df.shape)
控制台输出:
(2)数据表基本信息(维度、列名称、数据格式、所占空间等)
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv',header=None,usecols=[0,1,2,3]) #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题#columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
columns_name=['mysql_id','hotelname','customername','reviewtime']
df.columns=columns_nameprint(df.head(3)) #读取前3行
print(df.info())
控制台输出:
D:\softwaretools\anaconda\python.exe D:/pycharmprojects/hoteltest01/hoteltest01/test_csv_pandas.py
mysql_id hotelname customername reviewtime
0201 杭州马可波罗假日酒店 _***7 发表于2020-05-25
1 202 杭州马可波罗假日酒店 舟***猫 发表于2020-04-10
2 203 杭州马可波罗假日酒店 1***8 发表于2020-05-05
RangeIndex:50 entries, 0 to 49Data columns (total4columns):
mysql_id50 non-null int64
hotelname50 non-null object
customername50 non-null object
reviewtime50 non-null object
dtypes: int64(1), object(3)
memory usage:1.6+KB
None
(3)每一列数据的格式
利用df.dtypes
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv',header=None,usecols=[0,1,2,3]) #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题#columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
columns_name=['mysql_id','hotelname','customername','reviewtime']
df.columns=columns_nameprint(df.head(3)) #读取前3行
print(df.dtypes)
控制台输出:
D:\softwaretools\anaconda\python.exe D:/pycharmprojects/hoteltest01/hoteltest01/test_csv_pandas.py
mysql_id hotelname customername reviewtime
0201 杭州马可波罗假日酒店 _***7 发表于2020-05-25
1 202 杭州马可波罗假日酒店 舟***猫 发表于2020-04-10
2 203 杭州马可波罗假日酒店 1***8 发表于2020-05-05mysql_id int64
hotelname object
customername object
reviewtime object
dtype: object
(4)查看前3行数据、后3行数据
df.head() #默认前10行数据,注意:可以在head函数中填写参数,自定义要查看的行数
df.tail() #默认后10 行数据
importpandas as pd
df=pd.read_csv('hotelreviews50_1.csv',header=None,usecols=[0,1,2,3]) #hotelreviews50_1.csv文件与.py文件在同一级目录下#在读数之后自定义标题#columns_name=['mysql_id','hotelname','customername','reviewtime','checktime','reviews','scores','type','room','useful','likenumber']
columns_name=['mysql_id','hotelname','customername','reviewtime']
df.columns=columns_nameprint(df.head(3)) #读取前3行
print(df.tail(3)) #读取后3行
控制台输出: