ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/12/31 0031 下午 11:00 # @Author : 黄药师 # @desc : 这里写文件的功能说明 # @File : pandasAssign.py # @Software: PyCharm #pandas赋值及操作 import pandas as pd import numpy as np dates=np.arange(20170101,20170107) df1=pd.DataFrame(np.arange(24).reshape(6,4),index=dates,columns=['A','B','C','D']) print(df1) print(df1.iloc[2,2]) # 修改 df1.iloc[2,2]=100 print(df1.iloc[2,2]) # 注意是:loc,不是iloc df1.loc[20170102,'B']=200 print(df1) df1[df1.A>10]=0 print(df1) df1.A[df1.A==0]=1 print(df1) # 添加一列 df1['E']=10 print(df1) df1['F']=pd.Series([1,2,3,4,5,6],index=dates) print(df1) # 添加一行 df1.loc['20170107',['A','B','C']]=[1,2,3] print(df1) s1=pd.Series([1,2,3,4,5,6],index=['A','B','C','D','E','F']) s1.name='S2' df2=df1.append(s1) print(df2) # 插入列 # 在第一列插入索引为G的df2中的E列 df1.insert(1,'G',df2['E']) print(df1) # 移动列 g=df1.pop('G')#弹出列 df1.insert(6,'G',g)#在最后插入 print(df1) # 删除列 del df1['G'] print(df1) # 删除多列 df3=df1.drop(['A','B'],axis=1) print(df3) # 删除行 df4=df1.drop([20170102,20170101],axis=0) print(df4) ~~~