网站建设资讯

NEWS

网站建设资讯

python的old函数 python old函数

python pandas如何查找不同excel表格的数据并对比大小?

import pandas as pd

10年积累的网站建设、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有雁塔免费网站建设让你可以放心的选择与我们合作。

# 读取两张表格

new_df = pd.read_excel("本次成绩.xlsx")

old_df = pd.read_excel("上次成绩.xlsx")

# 拷贝一份要修改的数据,以免破坏源数据

ndf = new_df.copy()

# 首先将不在'上次成绩.xlsx'中的人直接修改'对比上次'字段为'上次缺席'

ndf['对比上次'][~ndf['姓名'].isin(old_df['姓名'].values)] = '上次缺席'

# 循环遍历'上次成绩.xlsx'中的每一行

for i in old_df.itertuples():

old_name = getattr(i, '姓名')

old_score = getattr(i, '上次成绩')

'''

当'本次成绩.xlsx'中的名字与 old_name 相同时

对比'本次成绩'与 old_score 的大小并修改'对比上次'为对应值

'''

ndf.loc[(ndf['姓名'] == old_name) (ndf['本次成绩'] old_score), '对比上次'] = '好'

ndf.loc[(ndf['姓名'] == old_name) (ndf['本次成绩'] == old_score), '对比上次'] = '持平'

ndf.loc[(ndf['姓名'] == old_name) (ndf['本次成绩'] old_score), '对比上次'] = '差'

# 导出到新表格并隐藏索引列

ndf.to_excel('对比.xlsx', index=False)

仅供参考,请根据实际情况自行修改优化代码。

python之字符串内置函数

1. 字符串字母处理

2. 字符串填充

str.ljust(width, fillchar)、str.center(width, fillchar)、str.rjust(width, fillchar)

返回一个指定的宽度 width 「居左」/「居中」/「居右」的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。

3,字符串计数

str.count(sub, start, end)

#统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

start, end遵循**“左闭右开”**原则。

4. 字符串位置

str.endswith(suffix, start, end)和str.startswith(substr, beg, end)

#判断字符串是否以指定后缀结尾/开头,如果以指定后缀「结尾」/「开头」返回 True,否则返回 False。

5. 字符串查找

6. 字符串判断

7. 字符串拼接

str.join() #将序列中的元素以指定的字符连接生成一个新的字符串。

s1 = "-" s2 = "" seq = ("r", "u", "n", "o", "o", "b")

# 字符串序列 print (s1.join( seq )) print (s2.join( seq )) r-u-n-o-o-b runoob

8. 统计字符串长度

str.len() #返回对象(字符、列表、元组等)长度或项目个数。

9. 去除字符两侧空格

str.lstrip()、str.rstrip()、str.strip() #截掉字符串「左边」/「右边」/「左右」两侧的空格或指定字符。

str0 = ' Hello World!' str0.lstrip() 'Hello World!' str1 = 'aaaa Hello World!' str1.lstrip('a') ' Hello World!'

10. str.maketrans(intab, outtab)和str.translate(table)

str.maketrans()创建字符映射的转换表

str.maketrans()根据参数table给出的表转换字符串的字符。

str.maketrans()传入的也可以是字典

tab = {'e': '3', 'o': '4'} trantab = str.maketrans(tab) str0.translate(trantab) 'H3ll4 W4rld!'

11. 字符串替换

str.replace(old, new, max)

12. 字符分割

str.split(str, num)

13. 字符填充

str.zfill(width)

返回指定长度的字符串,原字符串右对齐,前面填充0。

python 字符串替换问题

old = 'stsf'

pos = old.find('s')

if (pos != -1):

new = old[:pos+1] + old[pos+1:].replace('s', 'A', 1)

print new

else:

print "Substring 's' not found!"

用字符串切片。

下面是更通用些的代码(封装成函数)。

def replaceN(string, old, new, n):

''' Return a copy of the string with the 'n' occurrence of substring 'old' replaced by 'new'.

If substring 'old' is not found, original string is returned.

'''

if (n == 1): return string.replace(old, new, 1)

pos = -1; search = 0

while (search  n-1):

search += 1

pos = string.find(old, pos+1)

if (pos == -1): return string

return string[:pos+1] + string[pos+1:].replace(old, new, 1)

print replaceN('stsftst', 's', 'A', 2)

python的replace函数怎么用

Python replace()方法把字符串中的old(旧字符串)替换成new(新字符串),如果指定三个参数max,则替换不超过max次。

语法

replace()方法语法:

str.replace(old, new[, max])

参数

old -- 将被替换的子字符串;

new -- 新字符串,用于替换old子字符串;

max -- 可选字符串,替换不超过max次。

返回值

返回字符串中的old(旧字符串)替换成new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过max次。

实例

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";

print str.replace("is", "was");

print str.replace("is", "was", 3);

输出结果

thwas was string example....wow!!! thwas was really string

thwas was string example....wow!!! thwas is really string

python新手,python中的replace(old,new[,max]),编写过程中发现max为负数时,都进行了替换

max本来就是指定替换几个,负数就是无意义,跟没有一样,全部替换


网页标题:python的old函数 python old函数
转载源于:http://cdweb.net/article/dooshji.html