pandas
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import numpy as np import pandas as pd def main(): # 创建 # Serier 仅有index轴,DataFrame index与columns 行与列 index = pd.date_range( '1/1/2022' , periods = 8 ) a = pd.Series(np.random.randn( 5 ), index = [ 'a' , 'b' , 'c' , 'd' , 'e' ]) df = pd.DataFrame(np.random.randn( 8 , 3 ), index = index, columns = [ 'A' , 'B' , 'C' ]) print (a, df) # head tail print (df.head( 1 ), a.tail( 1 )) # head(n),tail 查看数据默认为5, print (a.array) # .array 属性用于提取 Index 或 Series 里的数据。 DataFrame无array print (df.index.array) # ['a', 'b', 'c', 'd', 'e'] print (df.values) # 查看数据 print (df.iloc[ 1 ]) # 取指定行 print (df.iloc[ 0 : 2 ]) # 指定行,0,1 print (df[ 'A' ]) # 指定列 df.to_csv( 'a.csv' ) # print(pd.read_csv("a.csv")) # df.reindex(index=['c', 'f', 'b'], axis='index') # 重置index,指定行 # df.reindex(index=['c', 'f', 'b'], axis='columns') # 重置index,指定列 # drop() 函数与 reindex 经常配合使用,该函数用于删除轴上的一组标签: # df.drop(['a', 'd'], axis=0) # df.drop(['one'], axis=1) #rename()方法支持按不同的轴基于映射(字典或 Series)调整标签。 print ( '---------------------------------------------------------' ) for i,j in df.items(): print (i,j) print (df.sort_values(by = 'a' )) #a列值 print (df.sort_index(axis = 0 )) # a列值 print (pd.read_scv(' '),pd.read_sql(' ')) if __name__ = = '__main__' : main() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)