社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  RJ Adriaansen  »  全部回复
回复总数  3
5 年前
回复了 RJ Adriaansen 创建的主题 » Python和Pandas:构造lambda参数

else 在lambda函数中不返回任何内容,因此它只返回True。我建议将它们组合在一个函数中,同时颠倒顺序:

composition_score_value = 40  #calculated by another process

def return_level(element):
    if element <= (composition_score_value *.699):
        return "Unsatisfactory work." # For scores below 69.
    elif element <= (composition_score_value *.799):
        return "Improvement needed." # For scores between 79 and 70.
    elif element <= (composition_score_value *.899):
        return "Satisfactory work." # For scores between 89 and 80.
    elif element <= (composition_score_value * 1):
        return "Good work." # For scores falling between 100 and 90 percent of composition_score_value.
    else:
        return None

df['composition_comment'] = df['composition_score'].apply(return_level)

结果:

作文分数 作文与评论
0 40 干得好。
1. 35 令人满意的工作。
2. 31 需要改进。
3. 27 令人不满意的工作。
5 年前
回复了 RJ Adriaansen 创建的主题 » 如何使用python删除csv中的换行符/新行

我建议在读取pandas中的csv之前删除多余的换行符。你可以打开文件,用 readlines() ,这将创建一个行列表。然后可以从列表中不包含三个换行符的每个项目中删除换行符 | 人物:

from io import StringIO
import pandas as pd

with open('problematic.csv') as f:
  text = f.readlines()
  text = ' '.join([i.replace('\n', '').strip() if i.count('|') <3 else i for i in text])

df = pd.read_csv(StringIO(text), sep='|',quotechar='"')

输出:

身份证件 名称 村庄 退休金
0 1. XYZ 拉姆 1500
1. 2. DINAL SHAMSUDH DHON 西瓦 2090

注意,这个例子假设 -----HEADER 不是csv文件的一部分。如果是,你可以用 replace() .

5 年前
回复了 RJ Adriaansen 创建的主题 » Python正则表达式提取电子邮件信息

你可以在没有正则表达式的情况下创建一个包含 splitlines 然后从主题行中分割出这个列表:

text = '''---------------------- Forwarded by Phillip K Allen/HOU/ECT on 03/21/2000 
01:24 PM ---------------------------
 
 
Stephane Brodeur
03/16/2000 07:06 AM
To: Phillip K Allen/HOU/ECT@ECT
cc:  
Subject: Maps
 
As requested by John, here's the map and the forecast...
Call me if you have any questions'''

data = text.splitlines()
slice_idx = [i for i, s in enumerate(data) if s.startswith('Subject: ')][0]
body = '/n'.join(data[slice_idx+2:])

输出:

As requested by John, here's the map and the forecast...
Call me if you have any questions