Chp4-2
2019 年 12 月 17 日
In [2]: import math my_list1=['haha',True, math.pi,56,7,8] print(my_list1[0]) print(my_list1[1]) print(my_list1[2]) print('\n') print(my_list1[-1]) haha True 3.141592653589793 8 In [3]: # 列表元素切取 print('\n') first_three=my_list1[:3] print(first_three) last_three=my_list1[-3:] print(last_three) without_first_last=my_list1[1:-1] print(without_first_last) y=my_list1[1:3] print(y) ['haha', True, 3.141592653589793] [56, 7, 8] [True, 3.141592653589793, 56, 7] [True, 3.141592653589793] In [4]: my_list1[0]=0 print(my_list1) [0, True, 3.141592653589793, 56, 7, 8] In [8]: my_list1=['haha',True, math.pi,56,7,8] print(my_list1) my_list1.append('hehe') print(my_list1) my_list1.insert(3,'你好! ') print(my_list1) ['haha', True, 3.141592653589793, 56, 7, 8] ['haha', True, 3.141592653589793, 56, 7, 8, 'hehe'] ['haha', True, 3.141592653589793, ' 你好! ', 56, 7, 8, 'hehe'] In [10]: # 元组 my_tuple_x=(1,2,3) my_tuple_y=4,5 print(my_tuple_x,my_tuple_y) try: my_tuple_x[1]='haha' except TypeError: print('不允许修改元组! ') print(my_tuple_x) (1, 2, 3) (4, 5) 不允许修改元组! (1, 2, 3) In [51]: #dict z={'haha':80,'wuwu':20} my_score=z['haha'] print(my_score) print('xixi' in z) 80 False In [52]: z['haha']=90 z['hehe']=10 print(z) print(z.keys()) print(z.values()) print(z.items()) {'haha': 90, 'wuwu': 20, 'hehe': 10} dict_keys(['haha', 'wuwu', 'hehe']) dict_values([90, 20, 10]) dict_items([('haha', 90), ('wuwu', 20), ('hehe', 10)]) In [1]: my_dict={1:20,2:30,4:50} print(my_dict) {1: 20, 2: 30, 4: 50} In [20]: #set my_set={'123','456',89,True} print(my_set) {89, '456', '123', True} In [12]: #ndarray import numpy as np x=[1,2,3,4,5,6] print(type(x)) print(x+x) <class 'list'> [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6] In [13]: my_ndarray1=np.array(x) type(my_ndarray1) Out[13]: numpy.ndarray In [15]: my_ndarray2=np.array([0,1,0,1,0,1]) my_sum=my_ndarray1+my_ndarray2 my_sum Out[15]: array([1, 3, 3, 5, 5, 7]) In [16]: a=my_sum.reshape((2,3)) print(a) print('\n') print(a[0,2]) [[1 3 3] [5 5 7]] In [20]: #dataframe import pandas as pd import numpy as np my_dataframe=pd.DataFrame(np.random.randn(4,5), index=['a','b','c','d'], columns=['A','B','C','D','E']) my_dataframe Out[20]: A B C D E a 1.385596 -0.646824 -0.247810 2.318796 -0.211721 b 0.452257 0.671649 0.615052 0.240041 0.470151 c 0.341342 1.604788 -0.885914 -0.796825 -0.619792 d 1.800393 1.076134 1.029607 -0.047988 2.193134 In [21]: type(my_dataframe) Out[21]: pandas.core.frame.DataFrame In [22]: my_dataframe[['B','C']] # 指定列访问 1,有列标题,返回值仍是 dataframe Out[22]: B C a -0.646824 -0.247810 b 0.671649 0.615052 c 1.604788 -0.885914 d 1.076134 1.029607 In [23]: my_dataframe[['B']] # 指定列访问 2,有列标题,返回值仍是 dataframe Out[23]: B a -0.646824 b 0.671649 c 1.604788 d 1.076134 In [24]: my_dataframe['B'] # 指定列访问 3,无列标题,返回值是序列 series Out[24]: a -0.646824 b 0.671649 c 1.604788 d 1.076134 Name: B, dtype: float64 In [25]: my_dataframe.iloc[1] # 指定行访问 1,指定行号 Out[25]: A 0.452257 B 0.671649 5C 0.615052 D 0.240041 E 0.470151 Name: b, dtype: float64 In [27]: my_dataframe.loc['b'] # 指定行访问 2,指定行索引 Out[27]: A 0.452257 B 0.671649 C 0.615052 D 0.240041 E 0.470151 Name: b, dtype: float64