【3】数据分析--10--科学计算--Pandas--3--Dataframe信息修改

一、例子

1.1 创建dataframe

import pandas as pd
import numpy as np

dates = pd.date_range('20170101', periods=8)
df = pd.DataFrame(np.random.randn(8,4), index=dates, columns=list('ABCD'))
print("df:")
print(df)
print('-'*50)

结果:

df:
                   A         B         C         D
2017-01-01 -0.598774  1.076390 -0.642006 -0.089715
2017-01-02 -0.438976  1.063627  0.387825  1.312049
2017-01-03  0.101879  0.469225  0.860522  0.086417
2017-01-04 -0.670031  1.974935 -0.570337  0.478371
2017-01-05  0.250046 -1.385470 -0.893637 -1.786031
2017-01-06  0.876446 -0.167285 -0.475356 -0.145381
2017-01-07  0.291258  0.676994 -1.953909 -0.609507
2017-01-08 -0.569716  0.749637  1.038614 -0.502682
--------------------------------------------------

1.2 新增一列

例1

s=pd.Series(list(range(10,18)),index=pd.date_range('20170101', periods=8))
df["F"]=s#新加一列元素F
print("df['F']=s")
print(df)
print('-'*50)

结果:

df['F']=s
                   A         B         C         D   F
2017-01-01 -0.598774  1.076390 -0.642006 -0.089715  10
2017-01-02 -0.438976  1.063627  0.387825  1.312049  11
2017-01-03  0.101879  0.469225  0.860522  0.086417  12
2017-01-04 -0.670031  1.974935 -0.570337  0.478371  13
2017-01-05  0.250046 -1.385470 -0.893637 -1.786031  14
2017-01-06  0.876446 -0.167285 -0.475356 -0.145381  15
2017-01-07  0.291258  0.676994 -1.953909 -0.609507  16
2017-01-08 -0.569716  0.749637  1.038614 -0.502682  17
--------------------------------------------------

例2 合并两列信息

dataframe["newColumn"] = dataframe["age"].map(str) + dataframe["phone"] + dataframe["address”]

1.3 修改某个数值

方法一: at

修改第一行,“A”列的数据

df.at[dates[0],"A"]=99
print("df.at[dates[0],'A']=99")
print(df)
print('-'*50)

df.at[dates[0],'A']=99
                    A         B         C         D   F
2017-01-01  99.000000  1.076390 -0.642006 -0.089715  10 
2017-01-02  -0.438976  1.063627  0.387825  1.312049  11
2017-01-03   0.101879  0.469225  0.860522  0.086417  12
2017-01-04  -0.670031  1.974935 -0.570337  0.478371  13
2017-01-05   0.250046 -1.385470 -0.893637 -1.786031  14
2017-01-06   0.876446 -0.167285 -0.475356 -0.145381  15
2017-01-07   0.291258  0.676994 -1.953909 -0.609507  16
2017-01-08  -0.569716  0.749637  1.038614 -0.502682  17
--------------------------------------------------

方法二: iat

修改第一行,第一列的数据

print("df.iat[1,1]=-66")
df.iat[1,1]=-66
print(df)
print('-'*50)
print("df.loc[:,'D']=np.array([4]*len(df))")

结果 df.iat[1,1]=-66 A B C D F 2017-01-01 99.000000 1.076390 -0.642006 -0.089715 10 2017-01-02 -0.438976 -66.000000 0.387825 1.312049 11 2017-01-03 0.101879 0.469225 0.860522 0.086417 12 2017-01-04 -0.670031 1.974935 -0.570337 0.478371 13 2017-01-05 0.250046 -1.385470 -0.893637 -1.786031 14 2017-01-06 0.876446 -0.167285 -0.475356 -0.145381 15 2017-01-07 0.291258 0.676994 -1.953909 -0.609507 16 2017-01-08 -0.569716 0.749637 1.038614 -0.502682 17 ————————————————–

方法三: loc

新增”D“列

df.loc[:,"D"]=np.array([4]*len(df))
print(df)
print('-'*50)
df2=df.copy()#拷贝
print('-'*50)

结果:

df.loc[:,'D']=np.array([4]*len(df))
                    A          B         C  D   F
2017-01-01  99.000000   1.076390 -0.642006  4  10
2017-01-02  -0.438976 -66.000000  0.387825  4  11
2017-01-03   0.101879   0.469225  0.860522  4  12
2017-01-04  -0.670031   1.974935 -0.570337  4  13
2017-01-05   0.250046  -1.385470 -0.893637  4  14
2017-01-06   0.876446  -0.167285 -0.475356  4  15
2017-01-07   0.291258   0.676994 -1.953909  4  16
2017-01-08  -0.569716   0.749637  1.038614  4  17
--------------------------------------------------
--------------------------------------------------

方法四

df2[df2>0]=-df2#将df2中的所有大于0的元素值 都改为小于0的
print (df2)

结果 A B C D F 2017-01-01 -99.000000 -1.076390 -0.642006 -4 -10 2017-01-02 -0.438976 -66.000000 -0.387825 -4 -11 2017-01-03 -0.101879 -0.469225 -0.860522 -4 -12 2017-01-04 -0.670031 -1.974935 -0.570337 -4 -13 2017-01-05 -0.250046 -1.385470 -0.893637 -4 -14 2017-01-06 -0.876446 -0.167285 -0.475356 -4 -15 2017-01-07 -0.291258 -0.676994 -1.953909 -4 -16 2017-01-08 -0.569716 -0.749637 -1.038614 -4 -17

二、讨论

用起来,是不是有点像R

参考资料

个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn

Sam avatar
About Sam
专注生物信息 专注转化医学