python的DataFrame排序问题
1.按照一列排序
- frame.sort_index(axis = 0,ascending = True,by = 'a') #升序
frame.sort_index(axis = 0,ascending = False,by = 'a') #降序
2. 按多列排序
frame.sort_index(axis = 0,ascending = True,by = ['b','c','a'])
3. 按行排序
frame.sort_index(axis = 1,ascending = True)
按照列排序后,index没有重新编写。需要将index重新设置
如下
- frame.sort_index(axis = 0,ascending = False,by = ['b','c','a'])
- Out[78]:
- a b c
- 1 2 7 5
- 0 9 4 6
- 5 7 2 4
- 4 0 2 4
- 3 1 2 3
- 2 5 -3 8
用 frame= frame.reset_index(drop=True) 来将index重新设置
也可以使用sort_values排序,老版本中为sort_index。
- import pandas as pd
- df = pd.DataFrame([[1,2,3],[2,3,4],[2,4,3],[1,3,7]],
- index = ['one','two','three','four'],columns = ['A','B','C'])
- print df
- # A B C
- # one 1 2 3
- # two 2 3 4
- # three 2 4 3
- # four 1 3 7
- df.sort_values(by=['A','B'],ascending=[0,1],inplace=True)
-
- #A为降序,b为升序,inplace=True代表结果按照排#序来,否则输出的df还是原来的样子
- print df
- # A B C
- # two 2 3 4
- # three 2 4 3
- # one 1 2 3
- # four 1 3 7
4. 在pandas的DataFrame中,我们经常需要根据某属性来选取指定条件的行,这时isin方法就特别有效。
- import pandas as pd
- df = pd.DataFrame([[1,2,3],[1,3,4],[2,4,3]],index = ['one','two','three'],columns = ['A','B','C'])
- print df
- # A B C
- # one 1 2 3
- # two 1 3 4
- # three 2 4 3
- mask = df['A'].isin([1]) #括号中必须为list
- print mask
- # one True
- # two True
- # three False
- # Name: A, dtype: bool
- print df[mask]
- # A B C
- # one 1 2 3
- # two 1 3 4
(八) dataframe.shape 返回一个元组包含df的维度信息即行数和列数,(row,col)
(九)字典存为csv,键值为表头
with open(datafilepath + "params.csv",'w') as f:
writer = csv.DictWriter(f,params.keys())
writer.writeheader()
writer.writerow(params)
(十)字典值排序和字典键名修改
排序: sorted(dict1,key = )
键名修改(键入键名为字符串)
dict2 = dict1.copy() 两字典不指向同一地址,修改dict1不会改变dict2(书上讲的是修改dict1会改变dict2,但我在使用过程中发现修改dict1不会改变dict2,修改dict2也不会改变dict1)
for key in dict2.keys():
dict1[key[:-4]] = dict1.pop(key) 修改键名删除原键名合为一句