Python Learning Note

  • python基本概念
    • python包
      • Pandas
  • Encountered errors and solutions:

python基本概念

python是一种解释型,面向对象,动态数据类型的高级程序设计语言.
(search for python extension packages to download extra packages)

python包

Pandas

In particular, pandas offers data structures and operations for manipulating numerical tables and time series:

  1. pd.Series
// create a series
In [3]: s = pd.Series([1, 1, 12, 6, np.nan])In [4]: s
Out[4]:
0    1.0
1    1.0
2    12.0
3    6.0
5    NaN
dtype: float64

Slice operation in series:
1\ To print elements from beginning to a range use [:Index] (does not include Index)
2\ To print elements till end-use [:-Index] (does not include Index)
3\ To print elements from specific Index till the end use [Index:] (does not include Index)
4\ To print elements within a range, use [Start Index:End Index]
5\ To print whole Series with the use of slicing operation, use [:]
6\ To print the whole Series in reverse order, use [::-1]

  1. pd.DataFrame:
// create a series
In [1]: df = pd.Series.to_frame(s)
In [2]: df.column = ['Rate']


//selecting columns:
1\ use .loc to do conditional selection.

data['Cost'].loc[data['DMA']=='Other'].sum()/data['Cost'].sum()
  1. dictionary
//create a dictionary
dict = {'Pheobe':[95,93,11,65],'luis':[99,97,66,70] }
  1. how to join two tables
df_off = df_off.set_index('trans_dt').join(total_gmb.set_index('trans_dt'),how='left', lsuffix='',rsuffix='_total', sort=False).reset_index()

Encountered errors and solutions:

1. datetime package:

AttributeError: 'datetime' module has no attribute 'strptime'

Solutions:

from datetime import datetime

or

#module  class    method
datetime.datetime.strptime(date, "%Y-%m-%d")

2. space in the string

In[5]: validation_data1['VERTICAL'].unique()
Out[5]: array(['Home\xa0&\xa0Garden', 'Other', 'Electronics'], dtype=object)

\xa0 is a non-breaking space in Latin1 (ISO 8859-1), also chr(160). Here is how to replace it with normal space:

string = string.replace(u'\xa0', u' ')

3. ‘pandas’ has no attribute ‘ewma’

    # 对size个数进行加权移动平均rol_weighted_mean = pd.ewma(timeSeries, span=size)改为rol_weighted_mean = pd.DataFrame.ewm(timeSeries, span=size).mean()

4. 解决plt图像交叠问题:

fig = plt.figure()
fig.tight_layout()
or
plt.tight_layout()


5. 怎么从string中选取特定字符

original array:
array(['Evergreen, Core, GEO1','PL_TS_Control, Evergreen, Promoted Listings','Evergreen, GEO4, Core', 'Evergreen, GEO2, Core','Evergreen, Core, GEO 5', 'Evergreen, GEO3, Core','Evergreen, PL_TS_Treatment, Promoted Listings','Trading Cards, Strategic', 'CR, Strategic','Evergreen, FBK, Core', 'Evergreen, Core, CTRL','Watches, Strategic', 'Watches_SSC_Treatment_GEOB, Strategic','Sneakers, Strategic', 'Watches_SSC_Control_GEOA, Strategic','Evergreen, Core', 'Evergreen, Core, C2C','Strategic, Sneaker_Showcase'], dtype=object)data_test=data.loc[data['Labels on Campaign'].apply(lambda x: (x.split(',',1)[1] == ' Strategic') or (x.split(',',1)[0] == 'Strategic'))]array(['Trading Cards, Strategic', 'CR, Strategic', 'Watches, Strategic','Watches_SSC_Treatment_GEOB, Strategic', 'Sneakers, Strategic','Watches_SSC_Control_GEOA, Strategic','Strategic, Sneaker_Showcase'], dtype=object)
  1. 浅拷贝和深拷贝
dict2 = dict1          # 浅拷贝: 引用对象
dict3 = dict1.copy()   # 浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是引用
  1. is 和 == 的区别
    ‘b is a’ returns ‘True’ when a and b point to the same object
    ‘b == a’ returns ‘True’ when a and b have the same variables
a = [1,2,3]
b = a                      #copy a
c = a[:]                   #copy a using slice operator
if b == a:print('True1')
if b is a:print('True2')
if c == a:print('True3')
if c is a:print('True4')[out]: True1True2True3

python learning note相关推荐

  1. Python Learning Notes - 2

    Python Learning Notes - 2 主体感想 我们要想表达出一个事件(event),或一系列相联系的事件(events series that construct a system w ...

  2. Python 基础 - Day 5 Learning Note - 模块 之 标准库:xml (9)

    xml 模块介绍 和json一样,适用于不同语言及程序的数据交换的协议.但是json用起来更简单,并有代替xml的趋势. 现在多数金融数据提供方(e.g. bloombegy)还在用xml的方式. 在 ...

  3. Python 基础 - Day 2 Learning Note - 字符转编码操作

    前情提要:字符编码 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill).ASCILL有255个字符,包括英语和西欧字符,不包括中文.汉字处理靠GBK, window ...

  4. Python 基础 - Day 2 Learning Note - 序列之字符串 String

    字符串(string)特点: 不可修改 独立的字符组成, 通过slice切片操作顺序访问 创建 '' or "" or str() aString = 'alex Chai' # ...

  5. 2021-9-30 Python Teaching Note

    文章目录 I. Random Talk II. In-Class Tasks Task 1. Join our QQ group Task 2. Join our Learning Express T ...

  6. 2014/09/30 Learning Note

    Vbird Linux: Vim Learning: http://linux.vbird.org/linux_basic/0310vi.php Bash Shell: http://linux.vb ...

  7. LUA Learning Note 4: 数据结构

    为什么80%的码农都做不了架构师?>>>    --数据结构是没有语言之分的,只不过不同的语言实现起来的语法不同. --创建一个简单的数组 local arr1 = {1,2,3,4 ...

  8. the day of python learning(考试解释)

    一,基础题. 1. 简述变量命名规范 #1.必须是字母,数字,下划线任意组合,不可以单数字#2.不可以是中文或拼音#3.命名得有可理解性#4.关键字不可以作为命名 2. 字节和位的关系 #一字节等于八 ...

  9. My python learning

    2.17 面向对象编程 1.学习了面向对象编程中的一些基础函数 1.1__init__: init是实例创建后被调用的,用以设置对象属性的初始值.依据需求看是否需要这个函数.init函数可对类进行初始 ...

  10. python learning day1

    输入输出 输入 a=input('提示内容') a的数据类型是字符 数据类型转换 int(a) 按回车前的都输入 输出 print()各参数说明: print([obj1,obj2,....][,se ...

最新文章

  1. windows系统杀掉explorer.exe进程后黑屏
  2. 如何调用形参数组_函数的参数
  3. Krona绘制物种或功能组成圈图
  4. SSL应用系列之二:为Web站点实现SSL加密访问
  5. 【python】 web开发入门
  6. mysql-5.6.16-win32_mysql-5.6.16-win32免安装配置方法
  7. Angular应用页面里_ngcontent属性的生成逻辑
  8. vs2010调试运行时弹出对话框:系统找不到指定文件
  9. 时间格式转换y-m-d
  10. 供应链管理优化和革新八大关键因素
  11. 人性的弱点 - 把握人际交往的关键
  12. 第五节 comware概述
  13. 安全厂商发布网络间谍平台ProjectSauron研究报告
  14. 技术分享| RTC通讯中常用的音频格式
  15. MCE | 单胺能 非单胺能对抑郁症的作用
  16. Excel 经纬度互相转换
  17. %s %*s %*.*s
  18. Pandas 元素选取
  19. 大数据入门--windows安装sqlite3
  20. 安全生产双重预防体系建设数字化解决方案

热门文章

  1. 剖析 Chrome 的小恐龙游戏
  2. 数据库 求闭包、求候选码、范式转换、最小依赖集、无损分解及保持函数依赖
  3. Win10 ipv4无网络访问权限的解决办法
  4. openssl_sign(): supplied key param cannot be coerced into a private key
  5. 8080端口被占用如何杀掉进程
  6. 利用接口和继承实现  求三角形 圆形面积 和以圆形为底的圆锥形的体积
  7. Brocade switch upgrade firmware
  8. Deepin安装教程
  9. 守望先锋代码_使用守望台自动刷新SQL Server容器
  10. 守望先锋四服务器修改,服务器刷新率提高 《守望先锋》终于不用死在墙角了...