python 读取csv pickle excel

import pandas as pd

import warnings
warnings.filterwarnings("ignore")

scientists = pd.read_csv('./data/scientists.csv')
names = scientists['Name']
print(names)

import os
if not os.path.exists('./output/scientists_names_series.csv'):
    names.to_csv('./output/scientists_names_series.csv')

if not os.path.exists('./output/scientists_names_series1.csv'):
    #分隔符变成星号
    names.to_csv('./output/scientists_names_series1.csv',sep='*')

if not os.path.exists('./output/scientists_names_series2.csv'):
    #序号去掉,就是把行号去掉
    names.to_csv('./output/scientists_names_series2.csv',index=False)


if not os.path.exists('./output/scientists_df1.csv'):
    scientists.to_csv('./output/scientists_df1.csv')

if not os.path.exists('./output/scientists_df2.csv'):
    scientists.to_csv('./output/scientists_df2.csv',index=False)

import pandas as pd
#s1 = pd.Series([43,56])
#print(s1)

scientists = pd.read_csv('./data/scientists.csv')
names = scientists['Name']
print(names)

import os

if not os.path.exists('./output/scientists_names_series.pickle'):
    names.to_pickle('./output/scientists_names_series.pickle')

if not os.path.exists('./output/scientists_df.pickle'):
    scientists.to_pickle('./output/scientists_df.pickle')

#读取pickle文件
scientists_name_from_pickle = pd.read_pickle('./output/scientists_names_series.pickle')
print(scientists_name_from_pickle)

scientists_name_from_pickle = pd.read_pickle('./output/scientists_df.pickle')
print(scientists_name_from_pickle)

在这里插入图片描述

#读写Excel 格式的Series和DataFrame文件
import pandas as pd
scientists = pd.read_csv('./data/scientists.csv')
names = scientists['Name']
#print(names)

names.to_excel('./output/scientists_names_series.xls')
#可以吧Series转成DataFrame
names_df = names.to_frame()
names_df.to_excel('./output/scientists_names_series1.xls')

#就是吧底下的sheet 改个名字
scientists.to_excel('./output/scientists_df.xlsx',sheet_name='scientists',index=False)


#读取excel文件
import xlrd
# 打开workbook  获取sheet
data = xlrd.open_workbook('./output/scientists_df.xlsx')
sheet = data.sheet_by_name('scientists')
sheet = data.sheet_by_index(0)
#第一行数据
print(sheet.row_values(1))
#第一个列
print(sheet.col_values(1))
print('行数','=',sheet.nrows)
print('列数','=',sheet.ncols)

print(sheet.cell(0,0).value)
#第二行,第三个 都是从0开始的
print(sheet.cell(2,3).value)

print(data.sheet_names())
print(sheet.name)

#获取一整行数据
print(sheet.row_values(3))
#获取一整列的数据
print(sheet.col_values(2))


在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟伟哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值