十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
import pandas as pd
10年積累的網站建設、成都做網站經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先做網站后付款的網站建設流程,更有雁塔免費網站建設讓你可以放心的選擇與我們合作。
# 讀取兩張表格
new_df = pd.read_excel("本次成績.xlsx")
old_df = pd.read_excel("上次成績.xlsx")
# 拷貝一份要修改的數(shù)據(jù),以免破壞源數(shù)據(jù)
ndf = new_df.copy()
# 首先將不在'上次成績.xlsx'中的人直接修改'對比上次'字段為'上次缺席'
ndf['對比上次'][~ndf['姓名'].isin(old_df['姓名'].values)] = '上次缺席'
# 循環(huán)遍歷'上次成績.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)
僅供參考,請根據(jù)實際情況自行修改優(yōu)化代碼。
1. 字符串字母處理
2. 字符串填充
str.ljust(width, fillchar)、str.center(width, fillchar)、str.rjust(width, fillchar)
返回一個指定的寬度 width 「居左」/「居中」/「居右」的字符串,如果 width 小于字符串寬度直接返回字符串,否則使用 fillchar 去填充。
3,字符串計數(shù)
str.count(sub, start, end)
#統(tǒng)計字符串里某個字符出現(xiàn)的次數(shù)。可選參數(shù)為在字符串搜索的開始與結束位置。
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. 統(tǒng)計字符串長度
str.len() #返回對象(字符、列表、元組等)長度或項目個數(shù)。
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()創(chuàng)建字符映射的轉換表
str.maketrans()根據(jù)參數(shù)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。
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!"
用字符串切片。
下面是更通用些的代碼(封裝成函數(shù))。
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()方法把字符串中的old(舊字符串)替換成new(新字符串),如果指定三個參數(shù)max,則替換不超過max次。
語法
replace()方法語法:
str.replace(old, new[, max])
參數(shù)
old -- 將被替換的子字符串;
new -- 新字符串,用于替換old子字符串;
max -- 可選字符串,替換不超過max次。
返回值
返回字符串中的old(舊字符串)替換成new(新字符串)后生成的新字符串,如果指定第三個參數(shù)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
max本來就是指定替換幾個,負數(shù)就是無意義,跟沒有一樣,全部替換