博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pandas 3 设置值
阅读量:4920 次
发布时间:2019-06-11

本文共 3635 字,大约阅读时间需要 12 分钟。

from __future__ import print_functionimport pandas as pdimport numpy as npnp.random.seed(1)dates = pd.date_range('20130101', periods=6)df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['A', 'B', 'C', 'D'])

赋值,新增列数据

df.iloc[2,2], df.loc['2013-01-03', 'D']

df.A[df.A>0], df['F']

df.iloc[2,2] = 1111                # 设置行列编号为2,2的数据只为1df.loc['2013-01-03', 'D'] = 2222   # 设置行属性值为‘2013……’,列属性值为‘D’的值为2222df[df.A>0] = 0    # 只保留列属性为‘A’且大于0的值,全部数据中的其他数据都设置为0df.A[df.A>0] = 0  # 只更改列属性为‘A’的数据df['F'] = np.nan  # 新增加一个属性列‘F’,所有的值为NaNdf['G']  = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130101', periods=6)) # 新增一个列‘G’

以下是所有的运行结果:

print(df)>                    A         B         C         D> 2013-01-01  1.624345 -0.611756 -0.528172 -1.072969> 2013-01-02  0.865408 -2.301539  1.744812 -0.761207> 2013-01-03  0.319039 -0.249370  1.462108 -2.060141> 2013-01-04 -0.322417 -0.384054  1.133769 -1.099891> 2013-01-05 -0.172428 -0.877858  0.042214  0.582815> 2013-01-06 -1.100619  1.144724  0.901591  0.502494
df.iloc[2,2] = 1111print(df)>                    A         B            C         D> 2013-01-01  1.624345 -0.611756    -0.528172 -1.072969> 2013-01-02  0.865408 -2.301539     1.744812 -0.761207> 2013-01-03  0.319039 -0.249370  1111.000000 -2.060141> 2013-01-04 -0.322417 -0.384054     1.133769 -1.099891> 2013-01-05 -0.172428 -0.877858     0.042214  0.582815> 2013-01-06 -1.100619  1.144724     0.901591  0.502494
df.loc['2013-01-03', 'D'] = 2222print(df)>                    A         B            C            D> 2013-01-01  1.624345 -0.611756    -0.528172    -1.072969> 2013-01-02  0.865408 -2.301539     1.744812    -0.761207> 2013-01-03  0.319039 -0.249370  1111.000000  2222.000000> 2013-01-04 -0.322417 -0.384054     1.133769    -1.099891> 2013-01-05 -0.172428 -0.877858     0.042214     0.582815> 2013-01-06 -1.100619  1.144724     0.901591     0.502494
df[df.A < 0] = 0print(df)>                    A         B         C         D> 2013-01-01  1.624345 -0.611756 -0.528172 -1.072969> 2013-01-02  0.865408 -2.301539  1.744812 -0.761207> 2013-01-03  0.319039 -0.249370  1.462108 -2.060141> 2013-01-04  0.000000  0.000000  0.000000  0.000000> 2013-01-05  0.000000  0.000000  0.000000  0.000000> 2013-01-06  0.000000  0.000000  0.000000  0.000000
df.A[df.A < 0] = 0print(df)>                    A         B         C         D> 2013-01-01  1.624345 -0.611756 -0.528172 -1.072969> 2013-01-02  0.865408 -2.301539  1.744812 -0.761207> 2013-01-03  0.319039 -0.249370  1.462108 -2.060141> 2013-01-04  0.000000 -0.384054  1.133769 -1.099891> 2013-01-05  0.000000 -0.877858  0.042214  0.582815> 2013-01-06  0.000000  1.144724  0.901591  0.502494
df['E'] = np.nanprint(df)>                    A         B         C         D   E> 2013-01-01  1.624345 -0.611756 -0.528172 -1.072969 NaN> 2013-01-02  0.865408 -2.301539  1.744812 -0.761207 NaN> 2013-01-03  0.319039 -0.249370  1.462108 -2.060141 NaN> 2013-01-04  0.000000 -0.384054  1.133769 -1.099891 NaN> 2013-01-05  0.000000 -0.877858  0.042214  0.582815 NaN> 2013-01-06  0.000000  1.144724  0.901591  0.502494 NaN
df['G']  = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130101', periods=6))print(df)>                    A         B         C         D   E  G> 2013-01-01  1.624345 -0.611756 -0.528172 -1.072969 NaN  1> 2013-01-02  0.865408 -2.301539  1.744812 -0.761207 NaN  2> 2013-01-03  0.319039 -0.249370  1.462108 -2.060141 NaN  3> 2013-01-04  0.000000 -0.384054  1.133769 -1.099891 NaN  4> 2013-01-05  0.000000 -0.877858  0.042214  0.582815 NaN  5> 2013-01-06  0.000000  1.144724  0.901591  0.502494 NaN  6

END

转载于:https://www.cnblogs.com/yangzhaonan/p/10435806.html

你可能感兴趣的文章
fmt 包中的函数和方法
查看>>
我所了解的一些路由器对比
查看>>
Yii2的深入学习--入口文件
查看>>
Python 多进程
查看>>
Android-Launcher开发之ShortCut(1)
查看>>
SharePoint 2013 图文开发系列之网站栏
查看>>
Sass Maps的函数
查看>>
Linux常用命令之Tmux
查看>>
ubunt1204安装配置vsftp
查看>>
Swift - UIView,UItableView,Cell设置边框方法
查看>>
jdbctemplate
查看>>
Centos7安装mysql-5.7.19
查看>>
ios中的coredata
查看>>
WPF控件库:文字按钮的封装
查看>>
N1 语法单词
查看>>
[转载]DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
查看>>
[zz]使用thrift做c++,java和python的相互调用
查看>>
使用debootstrap 命令
查看>>
folly 相关库学习
查看>>
PHP中magic_quotes_gpc动态关闭无效的问题
查看>>