• python dataframe (method,partial,dir,hasattr,setattr,getarrt)


    # * _*_ coding:utf-8 _*_
    __author__:'denny 20170730'
    from functools import reduce
    import functools
    import pandas as pd

    #create dataframe
    #df method
    #partial
    #dir,hasattr,setattr,getarrt

    def createdf():
    df = pd.DataFrame(
    {'a':[1,2,3],
    'b':[4,5,6],
    'c':[7,8,9]},
    index = [1,2,3])
    print(df)
    def createdfnoindex():

    df = pd.DataFrame(
    {'a':[1,2,3],
    'b':[4,5,6],
    'c':[7,8,9]},
    index = df.MultiIndex.from_tuples(
    [('d', 1), ('d', 2), ('e', 2)],
    names=['n', 'v']))


    def createadfcolumns():
    df0 = pd.DataFrame(
    {'a':[1,2,3],
    'b':[4,5,6],
    'c':[7,8,9]},
    index = [1,2,3])
    df = pd.DataFrame(
    [
    [1,2,3],
    [4,5,9],
    [7,8,]
    ],
    index =[1,2,3],
    columns=['a','b','c']
    )
    print(df0)
    print(df)
    #print(df.shape)
    print(pd.merge(df0, df))
    # print(df.dropna())
    #print(df.loc[0:1,['b']])
    #print(df.iloc[0:1][0:2])
    # print(df[0:2][df['a']>1])
    #print(df.iloc[0,2])
    #print(df.iloc[0:2,1:2])
    #print(df.loc['0',['a','b']])
    #print(df['a'].corr(df['c']))
    #print(df[0:2][0:1])
    #print(df.sort_index(axis=0,ascending=False))

    print('************')
    #createdf()
    #createadfcolumns()

    int2 = functools.partial(int,base=2)
    #print(int2('100000000'))
    #print(dir(str))
    #setattr(int2,'x',10)
    #print(hasattr(int2,'x'))
    #print(int2.x)

    ---------------python function test-----------------------
    def testprint():
    dictest()
    s = set((1, 2, 2, 3))
    s2 = ['b', 'a', 'c']
    print(s)
    print(s2.sort())
    print(hex(10))

    y = my_abs(3)
    print(y)
    ss = tuple(range(1, 11))
    s3 = [x + y for x in '123' for y in 'abc']
    print(s3)
    L = [x * x for x in range(10)]
    print(L)

    def dictest():
    d = {'a':'x','b':'y'}
    if 'a' in d:
    print('yes')
    else:
    pass

    def my_abs(x):
    if not isinstance(x,(int,float)):
    raise TypeError('bad numer')
    if x >= 0:
    return 1.0,2.23,4
    else:
    return -x,x-1,x

    print(d.get('a',-1))

    def fact(n):
    if n==1:
    return 1
    return n * fact(n-1)

    def testiteration():
    d={'a':1,'b':2}
    for x,y in enumerate(d.items()):
    print(x,y)

    def testgenerator():
    g = (x * x for x in range(10))
    for n in g:
    print(n)

    def fib(max):
    n,a,b=0,0,1
    while n<max:
    print(b)
    a,b=b,a+b
    n =n+1
    return 'done'

    def generatortest(x):
    n,a,b=0,0,1
    while n<x:
    yield b
    a,b=b,a+b
    n=n+1

    def testYieid(x):
    g=generatortest(x)
    while True:
    try:
    x = next(g)
    print( x)
    except StopIteration as e:
    print(e.value)
    break

    def genreratortest(i):
    if i==0:
    yield [1]
    elif i==1:
    yield [1,1]
    elif i==2:
    yield [1,2,1]
    else:
    yield [1, 3,3,1]


    def demogenrerator():
    for i in range(5):
    f = genreratortest(i)
    t = next(f)
    print(t)

    def add(x,y):
    return x + y

    def strreser(s):
    return {'0':0,'1':1,'2':2,'3':3}[s]

    def cptitle(s):
    c =s.capitalize()
    return c

    r = reduce(add,[1,2,3,4,5])
    l=list(map(str,[1,2,3,4,5]))
    l2=reduce(lambda x,y:x * 10 +y,[1,2,3,4,5])
    L1=['asdf','asdfs','edfd']


    def log(f):
    #@ functools.wraps(func)
    def test():
    print('call %s():' % f.__name__)
    return f()
    return test


    @ log
    def now():
    print('2015-3-25')


    now()


    def log2(f):
    def lg():
    print('testdsd')
    return now2()
    return lg

    @log2
    def now2():
    print('test derewa')


  • 相关阅读:
    Python定时任务sched(一)
    Python使用selenium进行爬虫(一)
    有关在python中使用Redis(二)
    有关JSOUP学习分享(一)
    jsoup爬虫,项目实战,欢迎收看
    有关在python中使用Redis(一)
    俄罗斯方块代码
    Android !No Launcher activity found!错误
    让jar程序在linux上一直执行(转)
    MyEclipse打包jar 并加入第三方包
  • 原文地址:https://www.cnblogs.com/csj007523/p/7264202.html
Copyright © 2020-2023  润新知