15-numpy笔记-莫烦pandas-3
2024-08-30 20:33:11
代码
import pandas as pd
import numpy as np dates = pd.date_range('20130101', periods=6)
df=pd.DataFrame(np.arange(24).reshape((6,4)),index=dates, columns=['A','B','C','D']) # 行数,列数,赋值
df.iloc[1,2] = 1111
df.loc['20130101','B'] = 2222
print('-1-')
print(df) df[df.A>4] = 0
print('-2-')
print(df) df.A[df.A>4] = 0
print('-3-')
print(df) # 添加列
df['F'] = np.nan
print('-4-')
print(df) df['E'] = pd.Series([1,2,3,4,5,6],index=pd.date_range('20130101',periods=6))
print('-5-')
print(df)
输出
-1-
A B C D
2013-01-01 0 2222 2 3
2013-01-02 4 5 1111 7
2013-01-03 8 9 10 11
2013-01-04 12 13 14 15
2013-01-05 16 17 18 19
2013-01-06 20 21 22 23
-2-
A B C D
2013-01-01 0 2222 2 3
2013-01-02 4 5 1111 7
2013-01-03 0 0 0 0
2013-01-04 0 0 0 0
2013-01-05 0 0 0 0
2013-01-06 0 0 0 0
-3-
A B C D
2013-01-01 0 2222 2 3
2013-01-02 4 5 1111 7
2013-01-03 0 0 0 0
2013-01-04 0 0 0 0
2013-01-05 0 0 0 0
2013-01-06 0 0 0 0
-4-
A B C D F
2013-01-01 0 2222 2 3 NaN
2013-01-02 4 5 1111 7 NaN
2013-01-03 0 0 0 0 NaN
2013-01-04 0 0 0 0 NaN
2013-01-05 0 0 0 0 NaN
2013-01-06 0 0 0 0 NaN
-5-
A B C D F E
2013-01-01 0 2222 2 3 NaN 1
2013-01-02 4 5 1111 7 NaN 2
2013-01-03 0 0 0 0 NaN 3
2013-01-04 0 0 0 0 NaN 4
2013-01-05 0 0 0 0 NaN 5
2013-01-06 0 0 0 0 NaN 6
最新文章
- 【爬虫】python之BeautifulSoup用法
- Android文件系统的结构
- 获取枚举类型的描述description
- HTML 的 <;div>; 和 <;span>; 标签
- html文本框(input)不保存缓存记录
- 浏览器中 for in 反射 对象成员 的差异
- IntelliJ IDEA对开发者的三大诱惑
- Fiddler抓取https设置及其原理
- 利用JS去做响应式布局
- Guava 教程1-使用 Google Collections,Guava,static imports 编写漂亮代码
- 【原创】大叔经验分享(18)hive2.0以后通过beeline执行sql没有进度信息
- ICSE 2018 论文
- python专题 --- 递归
- 动手动脑(lesson 8)
- elasticsearch 基本操作
- 7.final关键字.md
- Orchard模块开发全接触8:改造后台
- MySQL Crash Course #01# Chapter 1. 2 概念. Primary key
- SaltStack一键部署负载均衡和高可用
- KClient——kafka消息中间件源码解读