直接赋值的话,只是复制的元数据(行列索引),但是元素还是存储在相同内存位置 对元素进行修改会影响另外一个。
import pandas as pd import numpy as np df=pd.DataFrame(np.arange(12).reshape(null,3),index=list("abcd"),columns=['w','y','z']) print(df) print(df.iloc[1,2]) df.iloc[1,2]=20 print(df.iloc[1,2]) out: w y z a 0 1 2 b 3 4 5 c 6 7 8 d 9 10 11 5-------->>赋值之前 20-------->>赋值之后
copy函数,复制原数据(行列索引),还创建新的存储位置 对元素进行修改不影响另外一个。
df=pd.DataFrame(np.arange(12).reshape(null,3),index=list("abcd"),columns=['w','y','z']) print(df) df1=df.copy() print(df1.iloc[1,2]) df1.iloc[1,2]=20 print(df.iloc[1,2]) out: w y z a 0 1 2 b 3 4 5 c 6 7 8 d 9 10 11 5 5
通过[]操作符+列名方式增加多列 新增列在最后 df[['new_column1','new_column2',...]] =
通过loc+列名新增一列,不能新增多列 新增列在最后 pd.loc[:, 'new_column'] =
insert(loc, column, value, allow_duplicates=False)
import pandas as pd import numpy as np df=pd.DataFrame(np.arange(12).reshape(null,3),index=list("abcd"),columns=['w','y','z']) print(df) df['n']=[3,7,9,11] df[['x','k']]=df[['w','z']] df.loc[:,'r']=[12,13,15,16] df.insert(null,'t',[31,56,78,5])------>>增加在第一列 print(df) out: w y z a 0 1 2 b 3 4 5 c 6 7 8 d 9 10 11 w t y z n x k r a 0 31 1 2 3 0 2 12 b 3 56 4 5 7 3 5 13 c 6 78 7 8 9 6 8 15 d 9 5 10 11 11 9 11 16
新增行在最后 通过loc函数新增一行,不能新增多行 pd.loc['new-index'] =
import pandas as pd import numpy as np df=pd.DataFrame(np.arange(12).reshape(null,3),index=list("abcd"),columns=['w','y','z']) print(df) df.loc['r']=[12,13,15] print(df) out: w y z a 0 1 2 b 3 4 5 c 6 7 8 d 9 10 11 w y z a 0 1 2 b 3 4 5 c 6 7 8 d 9 10 11 r 12 13 15
Del 只能删除一列 ,语法:del df['column-name']
pd.drop() 可以删除多列
pd.drop(labels,axis=1, inplace=False)
pd.pop() 只能删除一列并把删除的一列赋值给新的对象。
import pandas as pd import numpy as np df=pd.DataFrame(np.arange(40).reshape(null,8),index=list("abcde"),columns=['w','y','z', 'l','m','n','o','p']) print(df) del df['w'] df.drop(labels=['y','l'],axis=1,inplace=True) print(df) data=df.pop('n') print(df) print(data) out: w y z l m n o p a 0 1 2 3 4 5 6 7 b 8 9 10 11 12 13 14 15 c 16 17 18 19 20 21 22 23 d 24 25 26 27 28 29 30 31 e 32 33 34 35 36 37 38 39 z m n o p a 2 4 5 6 7 b 10 12 13 14 15 c 18 20 21 22 23 d 26 28 29 30 31 e 34 36 37 38 39 z m o p a 2 4 6 7 b 10 12 14 15 c 18 20 22 23 d 26 28 30 31 e 34 36 38 39 a 5 b 13 c 21 d 29 e 37 Name: n, dtype: int32
重复值查看 duplicated(subset=None, keep='first’) ,Subset 是否只需要检查某几列
重复值删除
drop_duplicates(subset=None, keep=’first’, inplace=False)
import pandas as pd import numpy as np data=pd.DataFrame({'qu1':[1,3,4,3,4], 'qu2':[1,3,4,3,4], 'qu3':[1,3,2,3,3]}) print(data) print(data.duplicated(keep='first')) print(data.duplicated(keep='last')) print(data.drop_duplicates()) out: qu1 qu2 qu3 0 1 1 1 1 3 3 3 2 4 4 2 3 3 3 3 4 4 4 3 0 False 1 False 2 False 3 True 4 False dtype: bool 0 False 1 True 2 False 3 False 4 False dtype: bool qu1 qu2 qu3 0 1 1 1 1 3 3 3 2 4 4 2 4 4 4 3
索引的不可变性,不能对索引的某个值直接进行修改。
整体重命名 pd.index =, pd.columns =
import pandas as pd import numpy as np df=pd.DataFrame(np.arange(40).reshape(null,8),index=list("abcde"),columns=['w','y','z', 'l','m','n','o','p']) print(df.index) print(df.columns) df.index='new_'+df.index df.columns='new'+df.columns print(df.index) print(df.columns) out: Index(['a', 'b', 'c', 'd', 'e'], dtype='object') Index(['w', 'y', 'z', 'l', 'm', 'n', 'o', 'p'], dtype='object') Index(['new_a', 'new_b', 'new_c', 'new_d', 'new_e'], dtype='object') Index(['neww', 'newy', 'newz', 'newl', 'newm', 'newn', 'newo', 'newp'], dtype='object')
行列同时修改
rename(index=None, columns=None, **kwargs)
import pandas as pd import numpy as np df=pd.DataFrame(np.arange(40).reshape(null,8),index=list("abcde"),columns=['w','y','z', 'l','m','n','o','p']) print(df.index) print(df.columns) df.rename(index={'a':'a1','b':'b1'},columns={'w':'w1','l':'l1'},inplace=True) print(df.index) print(df.columns) out: Index(['a', 'b', 'c', 'd', 'e'], dtype='object') Index(['w', 'y', 'z', 'l', 'm', 'n', 'o', 'p'], dtype='object') Index(['a1', 'b1', 'c', 'd', 'e'], dtype='object') Index(['w1', 'y', 'z', 'l1', 'm', 'n', 'o', 'p'], dtype='object')
同时调整行或者列
reindex(index=None, columns=None, **kwargs)
# series reindex data1 = pd.Series(np.arange(4), index=list('ABCD')) print(s1) ''' A 1 B 2 C 3 D 4 dtype: int64 ''' # 重新指定 index, 多出来的index,可以使用fill_value 填充 print(s1.reindex(index=['A', 'B', 'C', 'D', 'E'], fill_value = 10)) ''' A 1 B 2 C 3 D 4 E 10 dtype: int64 ''' s2 = Series(['A', 'B', 'C'], index = [1, 5, 10]) print(s2) ''' 1 A 5 B 10 C dtype: object ''' # 修改索引, # 将s2的索引增加到15个 # 如果新增加的索引值不存在,默认为 Nan print(s2.reindex(index=range(15))) ''' 0 NaN 1 A 2 NaN 3 NaN 4 NaN 5 B 6 NaN 7 NaN 8 NaN 9 NaN 10 C 11 NaN 12 NaN 13 NaN 14 NaN dtype: object ''' # ffill : foreaward fill 向前填充, # 如果新增加索引的值不存在,那么按照前一个非nan的值填充进去 print(s2.reindex(index=range(15), method='ffill')) ''' 0 NaN 1 A 2 A 3 A 4 A 5 B 6 B 7 B 8 B 9 B 10 C 11 C 12 C 13 C 14 C dtype: object ''' # reindex dataframe df1 = DataFrame(np.random.rand(25).reshape([5, 5]), index=['A', 'B', 'D', 'E', 'F'], columns=['c1', 'c2', 'c3', 'c4', 'c5']) print(df1) ''' c1 c2 c3 c4 c5 A 0.700437 0.844187 0.676514 0.727858 0.951458 B 0.012703 0.413588 0.048813 0.099929 0.508066 D 0.200248 0.744154 0.192892 0.700845 0.293228 E 0.774479 0.005109 0.112858 0.110954 0.247668 F 0.023236 0.727321 0.340035 0.197503 0.909180 ''' # 为 dataframe 添加一个新的索引 # 可以看到 自动 扩充为 nan print(df1.reindex(index=['A', 'B', 'C', 'D', 'E', 'F'])) ''' 自动填充为 nan c1 c2 c3 c4 c5 A 0.700437 0.844187 0.676514 0.727858 0.951458 B 0.012703 0.413588 0.048813 0.099929 0.508066 C NaN NaN NaN NaN NaN D 0.200248 0.744154 0.192892 0.700845 0.293228 E 0.774479 0.005109 0.112858 0.110954 0.247668 F 0.023236 0.727321 0.340035 0.197503 0.909180 ''' # 扩充列, 也是一样的 print(df1.reindex(columns=['c1', 'c2', 'c3', 'c4', 'c5', 'c6'])) ''' c1 c2 c3 c4 c5 c6 A 0.700437 0.844187 0.676514 0.727858 0.951458 NaN B 0.012703 0.413588 0.048813 0.099929 0.508066 NaN D 0.200248 0.744154 0.192892 0.700845 0.293228 NaN E 0.774479 0.005109 0.112858 0.110954 0.247668 NaN F 0.023236 0.727321 0.340035 0.197503 0.909180 NaN ''' # 减小 index print(s1.reindex(['A', 'B'])) ''' 相当于一个切割效果 A 1 B 2 dtype: int64 ''' print(df1.reindex(index=['A', 'B'])) ''' 同样是一个切片的效果 c1 c2 c3 c4 c5 A 0.601977 0.619927 0.251234 0.305101 0.491200 B 0.244261 0.734863 0.569936 0.889996 0.017936 ————————————————
pd.sort_index(axis=1, ascending=False, inplace=True)
import pandas as pd import numpy as np df=pd.DataFrame(np.arange(9).reshape(null,3),index=list("acb"),columns=['w','m','z',]) print(df) df.sort_index(axis=0,ascending=True,inplace=True) print(df) out: w m z a 0 1 2 c 3 4 5 b 6 7 8 w m z a 0 1 2 b 6 7 8 c 3 4 5 df.sort_index(axis=1,ascending=True,inplace=True) print(df) out: m w z a 1 0 2 c 4 3 5 b 7 6 8
pd.sort_values(by='b', ascending=False, inplace=True)
import pandas as pd import numpy as np data=pd.DataFrame({'qu1':[1,7,41,3,4], 'qu2':[1,9,4,37,4], 'qu3':[1,12,25,3,37]}) print(data) data.sort_values(by='qu1',ascending=True,inplace=True) print(data) out: qu1 qu2 qu3 0 1 1 1 1 7 9 12 2 41 4 25 3 3 37 3 4 4 4 37 qu1 qu2 qu3 0 1 1 1 3 3 37 3 4 4 4 37 1 7 9 12 2 41 4 25
以上为个人经验,希望对您有所帮助。