💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # Series对象 ## 查 ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data = data, index = index) # 也支持索引和分片s[0],s[0:2] print(s) ~~~ 输出 ~~~ a 10 b 11 c 12 dtype: int64 ~~~ **也可以根据bool来查询** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data = data, index = index) mask=[True, False, True] print(s[mask]) ~~~ 输出 ~~~ a 10 c 12 dtype: int64 ~~~ **也可以根据Ioc和iloc来查询** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data = data, index = index) print(s.loc['b']) print(s.iloc[1]) ~~~ 输出 ~~~ 11 11 ~~~ ## 改 **拷贝改** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data = data, index = index) s1 = s.copy() s1['a'] = 100 print(s1) ~~~ 输出 ~~~ a 100 b 11 c 12 dtype: int64 ~~~ **replace改** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data = data, index = index) # inplace为false表示不会在原始基础上改,true会 replace = s.replace(to_replace=10, value=101, inplace=False) print(replace) ~~~ 输出 ~~~ a 101 b 11 c 12 dtype: int64 ~~~ **改索引** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data=data, index=index) print(s.index) s.index = ['a', 'b', 'd'] print(s) ~~~ 输出 ~~~ Index(['a', 'b', 'c'], dtype='object') a 10 b 11 d 12 dtype: int64 ~~~ **更改索引** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data=data, index=index) s.rename(index={'a': 'A'}, inplace=True) print(s) ~~~ 输出 ~~~ A 10 b 11 c 12 dtype: int64 ~~~ ## 增 **把2个合并** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data=data, index=index) series = pd.Series(data=[100, 110], index=['h', 'k']) # ignore_index=False表示保留原来的索引,不保留索引变为0,1,2 append = s.append(series, ignore_index=False) print(append) ~~~ 输出 ~~~ a 10 b 11 c 12 h 100 k 110 dtype: int64 ~~~ **新增一项** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data=data, index=index) s['j'] = 500 print(s) ~~~ 输出 ~~~ a 10 b 11 c 12 j 500 dtype: int64 ~~~ ## 删 **删除一行** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data=data, index=index) del s['a'] print(s) ~~~ 输出 ~~~ b 11 c 12 dtype: int64 ~~~ **删除多行** ~~~ import pandas as pd data = [10, 11, 12] index = ['a', 'b', 'c'] # 构造一个Series结构 s = pd.Series(data=data, index=index) # inplace为True,原来的值会改变,否则原来值不会改变要变量接收结果 s.drop(['b', 'c'], inplace=True) print(s) ~~~ 输出 ~~~ a 10 dtype: int64 ~~~ # DataFrame ## 创建 ~~~ import pandas as pd # 数据 data = [[1, 2, 3], [4, 5, 6]] # 索引 index = ['a', 'b'] # 列名 columns = ['A', 'B', 'C'] df = pd.DataFrame(data=data, index=index, columns=columns) print(df) ~~~ 输出 ~~~ A B C a 1 2 3 b 4 5 6 ~~~ ## 查询 **查询某一列** ~~~ print(df['A']) ~~~ 输出 ~~~ a 1 b 4 Name: A, dtype: int64 ~~~ **查询某个索引** 用iloc ~~~ print(df.iloc[0]) ~~~ 输出 ~~~ A 1 B 2 C 3 Name: a, dtype: int64 ~~~ 用loc ~~~ print(df.loc['a']) ~~~ 输出 ~~~ A 1 B 2 C 3 Name: a, dtype: int64 ~~~ **索引和列定位一个数据** ~~~ print(df.loc['a']['A']) ~~~ 输出 ~~~ 1 ~~~ ## 更改 **更改某个值** ~~~ df.loc['a']['A'] = 150 print(df) ~~~ 输出 ~~~ A B C a 150 2 3 b 4 5 6 ~~~ **更改索引** ~~~ df.index = ['f', 'g'] print(df) ~~~ 输出 ~~~ A B C f 1 2 3 g 4 5 6 ~~~ ## 增加 **增加一列** ~~~ df.loc['c'] = [1, 2, 3] print(df) ~~~ 输出 ~~~ A B C a 1 2 3 b 4 5 6 c 1 2 3 ~~~ **合并2个DataFrame** ~~~ import pandas as pd # 数据 data = [[1, 2, 3], [4, 5, 6]] # 索引 index = ['a', 'b'] # 列名 columns = ['A', 'B', 'C'] # 创建个DataFrame结构 df = pd.DataFrame(data=data, index=index, columns=columns) df2 = pd.DataFrame(data=data, index=['j', 'k'], columns = columns) # 默认axis为0,按照样本拼接,也可以指定为1,但是没有的值会显示NaN df3 = pd.concat([df, df2], axis=0) print(df3) ~~~ 输出 ~~~ A B C a 1 2 3 b 4 5 6 j 1 2 3 k 4 5 6 ~~~ **增加一列** ~~~ # 增加列 df['Tang'] = [10, 11] print(df) ~~~ 输出 ~~~ A B C Tang a 1 2 3 10 b 4 5 6 11 ~~~ ## 删除 **删除一个索引** ~~~ # 删除一个索引 df.drop(['a'], axis=0, inplace=True) print(df) ~~~ 输出 ~~~ A B C b 4 5 6 ~~~ **删除一列** ~~~ # 删除一个列 del df['A'] print(df) ~~~ 输出 ~~~ B C a 2 3 b 5 6 ~~~ **删除多个列** ~~~ df.drop(['A', 'B'], axis=1, inplace=True) print(df) ~~~ 输出 ~~~ C a 3 b 6 ~~~