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()

  

posted on   HHMLXL  阅读(17)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示