pandas 读取csv 0开头的字符串
0开头的数字字符串,比如012355,会读成12355。
因此需要走下数据转换 converters={'code': str}可以对数据进行转换
code_data = pd.read_csv(file, encoding='utf8', converters={'code': str})
code是列名,需要换成你的文件列名,读取时会对那一列进行处理。
pandas 随机抽取1000行数据:
data = data.sample(n=1000,random_state=1) #随机抽取1000行
pandas获取数据尺寸信息:
获取数据总量和维度信息:
print(df_label.size,df_label.shape)
如果df_label是3行5列,那么:
结果是15 (3, 5)
获取行数:
print(df_label.__len__())
print(df_label.shape[0])
pandas 遍历速度比较
aa=1
start=time.time()
for row_index,data in enumerate(df_label.itertuples()):
aa+=1
print("aa time",aa,time.time()-start)
aa = 1
for row_index, data in enumerate(df_label.iterrows()):
aa += 1
print("bb time",aa, time.time() - start)
aa = 1
for row_index, data in df_label.iterrows():
aa += 1
print("cc time",aa, time.time() - start)
结果:
aa time 79039 0.43267178535461426
bb time 79039 6.147785902023315
cc time 79039 11.870731353759766
筛选数据再遍历速度比较:
aa=1
start=time.time()
df_datas = df_label[df_label['group_id'] == 5]
for row_index,data in enumerate(df_datas.itertuples()):
aa+=1
print("aa time",aa,time.time()-start)
aa = 1
for row_index, data in enumerate(df_datas.iterrows()):
aa += 1
print("bb time",aa, time.time() - start)
aa = 1
for row_index, data in df_datas.iterrows():
aa += 1
print("cc time",aa, time.time() - start)
结果:
aa time 6555 0.03705906867980957
bb time 6555 0.5034129619598389
cc time 6555 0.9736764430999756
总结:
itertuples速度最快,是iterrows速度的20-30倍。
————————————————
版权声明:本文为CSDN博主「AI视觉网奇」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jacke121/article/details/127085729