python dataframe (method,partial,dir,hasattr,setattr,getarrt)
Python method dataframe Dir partial
2023-09-11 14:13:58 时间
# * _*_ 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陷阱和注意点
- Python脚本扫描给定网段的MAC地址表(scapy或 python-nmap)
- Python中python-nmap模块的使用
- 【Python】ValueError: unsupported pickle protocol: 5解决方案/同一个项目 python环境尽量保持一致
- python TypeError: 'builtin_function_or_method' object is not iterable keys
- Python kafka操作实例(kafka-python)
- Python 问题 unindent does not match any outer indentation level 在 pycharm 平台的解决方法
- 掌握Python语言能做什么?python对小白友好吗?
- python验证IP的有效性及其是否属于内外网
- 35岁了转行python可以吗?什么样的人合适学习Python?
- 每天一个python小知识——如何在Python 3中转换数据类型
- 103 python高级 - 内建属性
- Python 格林威治时间格式 Wed Jul 10 20:00:09 +0800 2019 转换为标准时间格式年月日时分秒%Y-%m-%d %H:%M:%S
- Python 相对路径和绝对路径--python实战(九)
- python Python程序的架构
- 「基于Python技术的智慧中医商业项目」基于机器学习的Django问诊系统展示
- Python Streamlit教程大全之 02 获取网络数据数据,缓存网络数据(教程含源码)
- Python 实现被动收入教程之我如何使用 python 制作我的第一个高级telegram机器人
- Jenkins持续集成实战之Jenkins构建Python项目提示:'python' 不是内部或外部命令,也不是可运行的程序。
- 《Python自动化运维:技术与最佳实践》一导读
- python之设置控制台字体颜色
- 【Python】python 库分类大全
- Python 数组
- python换行