import pandas as pd
path =r"C:/Users/Augustine/Desktop/us-countries/pandas.xlsx" # 'r'
Data=pd.DataFrame({'序号':[1,2,3],'姓名':['张三','赵四','王五']})
Data.to_excel(path)
print("创建成功")
read_csv 使用“逗号”作为分隔符
read_table 使用“制表符” 作为分隔符
pd.read_table(path,sep=‘,’) 将"逗号"设置为分隔符
import pandas as pd
path ="C:/Users/Augustine/Desktop/us-countries/test.txt"
Data_read=pd.read_csv(path)
print(Data_read)
header 和names要同时使用
import pandas as pd
path ="C:/Users/Augustine/Desktop/us-countries/test.txt"
Data_read=pd.read_csv(path,header=None,names=['性别','姓名','年龄','手机','地址','入职时间'])
print(Data_read)
import pandas as pd
path ="C:/Users/Augustine/Desktop/us-countries/test.txt"
Data_read=pd.read_csv(path,header=None,names=['性别','姓名','年龄','手机','地址','入职时间'],index_col=['入职时间'])
print(Data_read)
skiprows 掠过某行
import pandas as pd
path ="C:/Users/Augustine/Desktop/us-countries/test.txt"
Data_read=pd.read_csv(path,header=None,names=['性别','姓名','年龄','手机','地址','入职时间'],index_col=['入职时间'],skiprows=[2,3])
print(Data_read)
nrows 只显示某几行
import pandas as pd
path ="C:/Users/Augustine/Desktop/us-countries/test.txt"
Data_read=pd.read_csv(path,header=None,names=['性别','姓名','年龄','手机','地址','入职时间'],index_col=['入职时间'],nrows=3)
print(Data_read)
import pandas as pd
Data=pd.read_csv("C:/Users/Augustine/Desktop/us-countries/test.txt")
Data.to_csv("C:/Users/Augustine/Desktop/us-countries/test.csv")
print(Data)
import pandas as pd
path ="C:/Users/Augustine/Desktop/us-countries/pandas.xlsx"
Data_read=pd.read_excel(path)
print(Data_read)
Data_read.to_excel(path) #将其写入