私信  •  关注

wjandrea

wjandrea 最近创建的主题
wjandrea 最近回复了
2 年前
回复了 wjandrea 创建的主题 » 在数据帧中找到最常见的组合——Python

一旦你 count duplicate rows ,只需要做一些工作就可以得到相应的标签。

以下是我将如何做到这一点,尽管我对熊猫不太熟悉,所以可能有更好的方法。首先,df应该是布尔值。

import pandas as pd

df = pd.DataFrame({
    'AA': [1, 0, 0, 1, 1],
    'BB': [0, 0, 1, 0, 0],
    'CC': [1, 1, 0, 1, 1]}
    ).astype(bool)

# Count duplicate rows
counts = df.groupby(df.columns.tolist()).size()
# Get most common rows
maxima = counts[counts==counts.max()]
for combination, count in maxima.iteritems():
    # Select matching labels
    labels = df.columns[list(combination)]
    print(*labels, count)

输出:

AA CC 3

部分结果:

>>> counts
AA     BB     CC   
False  False  True     1
       True   False    1
True   False  True     3
dtype: int64

>>> maxima
AA    BB     CC  
True  False  True    3
dtype: int64
3 年前
回复了 wjandrea 创建的主题 » 为什么在这个python脚本中声明的顺序不是一个问题

你说得对, MAP 只是在抬头的时候 __init__ 正在运行。

如果这有助于你理解,让我们去掉所有不必要的信息:

  1. __初始化__ 是一种方法。你可以用函数得到同样的行为。
  2. 地图 不重要,我们只想检查它是否存在
  3. 让我们看看当 不存在,使用除其他外的尝试。
  4. 把一切都写在一个剧本里
  5. self.Bar x
def foo():
    try:
        MAP
    except NameError:
        print('No!')
    else:
        print('Yes!')

foo()  # -> No!
MAP = 0
foo()  # -> Yes!

这些不是索引,而是格式说明符。 s 意思是“字符串”和 d 意思是“十进制数”。前面的数字表示要使用多少空白来对齐。这些定义在 Format Specification Mini-Language

如果只需要替换连续的重复(例如。 [1, 1] [1, 1, 0, 1] 但不是 [1, 0, 1] ),你可以使用 itertools.groupby

from itertools import groupby

def replace_repeats_with_stars(list_in):
    """
    >>> replace_repeats_with_stars([1, 1])
    ['*', '*']
    >>> replace_repeats_with_stars([1, 1, 0, 1])
    ['*', '*', 0, '*']
    >>> replace_repeats_with_stars([1, 0, 1])
    [1, 0, 1]
    """
    repeats = {k for k, g in groupby(list_in) if len(list(g)) > 1}
    return ['*' if x in repeats else x for x in list_in]
4 年前
回复了 wjandrea 创建的主题 » 只有一个参数时Python抛出多个参数错误

实例方法作为第一个参数隐式传递给实例( self ). 那意味着 crawler.crawl(web) 变成 WebCrawler.crawl(crawler, web) .

我不知道如何修复它,因为我不熟悉这些模块,但我想 crawl WebCrawler 没有 root

def crawl(self, arg):
    root = arg.root()
    ...
4 年前
回复了 wjandrea 创建的主题 » Append函数在python中没有给出所需的结果

这会产生所需的输出,尽管我仍然不了解目标。

pos_labels = ['abc', 'def', 'ab3', 'ab4', 'ab5']
sen_pos_list = [
    ('abc', 'def', 'ghi'),
    ('jkl', 'mno', 'pqr', '123'),
    ('stu', 'vwx')
    ]

sen_vec_list = [[int(p in s) for p in pos_labels] for s in sen_pos_list]

print(sen_vec_list)
4 年前
回复了 wjandrea 创建的主题 » Python3:os.system未重定向stdout

我不知道为什么,但看起来 os.system 使用的是Dash(Ubuntu的默认脚本shell),而不是Bash,所以 &> 不支持。相反,会发生的情况是命令被反接地,文件被截断。也就是说, command &> filename 相当于 command &; > filename .

> filename 2>&1 .

4 年前
回复了 wjandrea 创建的主题 » 如何在Python中引用字典的特定部分

在那个片段中, movies 是一个包含dict的列表。因此索引该列表,然后索引dict:

movies[0]['meterScore']

如果 电影 可能包含多个项(或零),请对其进行迭代以获取 meterScore 学生:

meter_scores = [movie['meterScore'] for movie in movies]
5 年前
回复了 wjandrea 创建的主题 » python argparse:通过帮助输出在参数组之间添加节?

正如评论中提到的,你想要 ArgumentParser.add_argument_group() .

在此复制示例代码,修改:

import argparse

parser = argparse.ArgumentParser(prog='PROG')

group = parser.add_argument_group('group')
group.add_argument('--foo', help='foo help')
group.add_argument('bar', help='bar help')

other_group = parser.add_argument_group('other group')
other_group.add_argument('baz', help='baz help')

parser.print_help()

输出:

usage: PROG [-h] [--foo FOO] bar baz

optional arguments:
  -h, --help  show this help message and exit

group:
  --foo FOO   foo help
  bar         bar help

other group:
  baz         baz help
4 年前
回复了 wjandrea 创建的主题 » python在两个字符之间替换一个字符并保持rest

你想要正则表达式 ,用括号创建,然后用反斜杠和数字引用:

>>> re.sub(r'(§[^,]+),([^,]+§)', r'\1;\2', '§Bandra(West),Mumbai§')
'§Bandra(West);Mumbai§'

在这里 (§[^,]+) 对应于 \1 ([^,]+§) 对应于 \2 .

更多信息: re - Python documentation 在下面 (...) \number

顺便说一句:

  • 你有一堆不必要的睫毛,我去掉了。
  • 在这个特定的示例中,可以使用更简单的模式:

    >>> re.sub(r'(§.*),(.*§)', r'\1;\2', '§Bandra(West),Mumbai§')
    '§Bandra(West);Mumbai§'
    

    甚至一个简单的 .replace :

    >>> '§Bandra(West),Mumbai§'.replace(',', ';')
    '§Bandra(West);Mumbai§'
    
4 年前
回复了 wjandrea 创建的主题 » python:typeerror:需要整数..卡住了

在python 3中,分割两个int总是产生一个float,因此 80/2 50/2 正在生产浮子,而不是Int。要使它们成为整数,您可以使用floor division( 80//2 )或转换为int( int(80/2) )

我想您的教程是针对python 2的,因为在python2中,分割两个int总是产生一个int。

更多细节