pandas设置值更改值

Posted 棍子哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pandas设置值更改值相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/5/24 15:03
# @Author  : zhang chao
# @File    : s.py
from scipy import linalg as lg
#按标签选择
#通过标签选择多轴

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)
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.at[dates[0],"A"]=99
print("df.at[dates[0],‘A‘]=99")
print(df)
print(-*50)
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.loc[:,"D"]=np.array([4]*len(df))
print(df)
print(-*50)
df2=df.copy()#拷贝
print(-*50)
print("")
df2[df2>0]=-df2#将df2中的所有大于0的元素值 都改为小于0的
print (df2)
D:\Download\python3\python3.exe D:/Download/pycharmworkspace/s.py
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
--------------------------------------------------
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
--------------------------------------------------
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
--------------------------------------------------
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
--------------------------------------------------
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
--------------------------------------------------
--------------------------------------------------

                    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

Process finished with exit code 0

 

以上是关于pandas设置值更改值的主要内容,如果未能解决你的问题,请参考以下文章

pandas的学习3-设置值

如何更改片段中的主要活动变量

Python:numpy/pandas 根据条件更改值

Pandas:根据不同类型的多个条件更改值

检查列中是不是存在值并根据不同条件在另一个 Pandas 中更改:Pandas

Python pandas:如何在 2 个不同类别之间更改分类值