导入科学工具包 pandas API接口:https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html#
import pandas as pd
print(pd.__version__)
import pandas as pd
df_excel = pd.read_excel(r'G:\py\test\demo.xlsx')
a = df_excel.head(5)
print(a)
#pandas 读取默认把第一行作为表头,由header参数控制
import pandas as pd
df_excel = pd.read_excel(r'G:\py\test\demo.xlsx')
a = df_excel.head(5)
print(a)
df_excel.to_excel(r'G:\py\test\savetest.xlsx',sheet_name='savesheet1')
df_excel.to_excel(r'G:\py\test\savetest.xlsx',sheet_name='savesheet2')
writer = pd.ExcelWriter(r'G:\py\test\xieruduogesheet.xlsx')
df_excel.to_excel(writer,sheet_name='savesheet3')
df_excel.to_excel(writer,sheet_name='savesheet4')
writer.save()
with pd.ExcelWriter(r'G:\py\test\测试会话式保存多个sheet的Excel文件数据.xlsx') as writer:
df_excel.to_excel(writer,sheet_name='测试Sheet5')
df_excel.to_excel(writer,sheet_name='测试Sheet6')
df_excel.to_excel(writer,sheet_name='测试Sheet7')
df_csv = pd.read_csv(r'G:\py\test\CSVTEST.csv')
df_csv.to_csv(r'G:\py\test\测试保存csv.csv')
df_csv.to_csv(r'G:\py\test\测试保存csv.csv',mode='a',header=None)
read_csv()
df_csv = pd.read_csv(r'G:\py\test\测试保存csv.csv',header= None)
df_csv.head()
list(“字符串”)把字符串序列化
import numpy as np
import pandas as pd
def main():
s = pd.Series([i*2 for i in range(1,11)])
print(type(s))
dates = pd.date_range("20170301",periods=8)
df = pd.DataFrame(np.random.randn(8,5),index = dates,columns = list("ABCDEF"))
print(df)
if __name__ == "__main__":
main()
df.decribe()
df[:3]
df.loc(dates[0])
df.loc(["20210701":"20210703",["B","D"]])
df.as[dates[0],"C"]
df.iloc[1:3,2:4]
df.iloc[1,4]
df.iat[1,4]
df[df.B>0][df.A<0]
df[df>0]
isin类似sql中的in
df[df["E"].isin([1,2])]
s1 = pd.Series(list(range(10,18)),index = pd.date_range("20210703",periods=8))
df["F"] = s1
print(df)
df.at[dates[0],"A"] = 0
print(df)
df.iat[1,1] = 1
df.loc[:"D"] = np.array([4]*len(df))
|