小编And*_*eas的帖子

Pandas:语言环境格式在 style.format() 中不起作用

我想pandas.Dataframe用 locale渲染一个"de_DE.UTF-8",有一个“,”作为小数点和“。” 作为千位分隔符。简单地运行时locale.format,我得到了预期的结果。但是当添加与 Pandas 格式化程序相同的表达式时,渲染的 html 没有任何变化(尽管没有抛出错误)。示例代码:

import pandas
import locale

locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
print(locale.format('%.2f', 1000.245, True))
print(locale.format('%.2f', 10000000.22655, True))

df = pandas.DataFrame({'a': [1000.245, 10000000.22655]})
style = df.style.format(formatter=lambda x: f'{locale.format("%.2f", x, True)} €')
print(style.render())
Run Code Online (Sandbox Code Playgroud)

给出输出:

1.000,25
10.000.000,23
<style  type="text/css" >
</style>  
<table id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56" > 
<thead>    <tr> 
        <th class="blank level0" ></th> 
        <th class="col_heading level0 col0" >a</th> 
    </tr></thead> 
<tbody>    <tr> 
        <th id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56level0_row0" class="row_heading level0 row0" >0</th> 
        <td id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56row0_col0" class="data row0 col0" >1000.25 €</td> 
    </tr>    <tr> 
        <th id="T_f0ae1678_3e71_11e9_8f47_d0bf9ce00d56level0_row1" …
Run Code Online (Sandbox Code Playgroud)

python formatting locale pandas

6
推荐指数
1
解决办法
1632
查看次数

如何根据生成的列表数据制作 pandas 数据框

我有一份共同作者名单:

ten_author_pairs = [('creutzig', 'gao'),
 ('creutzig', 'linshaw'),
 ('gao', 'linshaw'),
 ('jing', 'zhang'),
 ('jing', 'liu'),
 ('zhang', 'liu'),
 ('jing', 'xu'),
 ('briant', 'einav'),
 ('chen', 'gao'),
 ('chen', 'jing')]
Run Code Online (Sandbox Code Playgroud)

从这里我可以生成一个反例列表 - 即使用以下代码未连接的作者对:

#generating negative examples - 

from itertools import combinations

elements = list(set([e for l in ten_author_pairs for e in l])) # find all unique elements

complete_list = list(combinations(elements, 2)) # generate all possible combinations

#convert to sets to negate the order

set1 = [set(l) for l in ten_author_pairs]
complete_set = [set(l) for l in complete_list] …
Run Code Online (Sandbox Code Playgroud)

python graph list dataframe pandas

5
推荐指数
1
解决办法
154
查看次数

复制文件的原始创建日期

我有以前生成的文件的副本,并且想要检测该原始文件的创建日期。

我可以使用以下代码来获取该副本的创建日期,但不能获取原始文件的创建日期:

import os.path, time
print("last modified: %s" % time.ctime(os.path.getmtime(file)))
print("created: %s" % time.ctime(os.path.getctime(file)))
Run Code Online (Sandbox Code Playgroud)

来源:http ://www.aitek.ch/how-to-get-file-creation-modification-date-times-in-python/

但这只会检索副本的创建日期,而不是原始文件的创建日期。我读过,如果您在 Windows 上复制文件,旧的创建日期将是新的修改日期,副本的日期将是新的创建日期。我还读到,在metda日期中可能有一个真正的原始文件创建日期,但似乎我无法找到它。

python

4
推荐指数
1
解决办法
7139
查看次数

在python中将列表中的值添加到字典中

假设我有一个列表 li=['project1','project2','project3'] 我想将这些值添加为字典的键,并且第一个键的值应该被完成,暂停

项目可以是任何一个,列表中第一个项目的值应该完成,需要等待输出:

dict={'project1':'completed','project2':'onhold','project3':'onhold'}
Run Code Online (Sandbox Code Playgroud)
li=['project1','project2','project3']
for trav_cont in li:
   dict[trav_cont]='completed'  
Run Code Online (Sandbox Code Playgroud)

python

1
推荐指数
1
解决办法
69
查看次数

.format() 字符串,如果没有可用的匹配项,则保留大括号

我尝试使用大括号格式化字符串,如下所示:

dct_mapper = dict(a=1)
s = '{key1: value2, key1: value2}; attrib1={a}; attrib2={b}'
s.format(**dct_mapper)

Results in:
# KeyError: 'key1'

Expected:
# '{key1: value2, key1: value2}; attrib1=1; attrib2={b}'
Run Code Online (Sandbox Code Playgroud)

另一个限制是我需要.format_map()稍后再次使用输出,这会搞砸使用 .format() 的解决方案,因为输出大括号会消失。

我尝试了collections包中的 defaultdict 以及.format_map(),然后使用正则表达式尝试用其他括号替换括号,这感觉不太像一个解决方案,更像是一个黑客,如果你有多个,也不起作用字符串开头重复括号。

它不是 json 字符串,因为这样我就可以使用 json 库来映射值。

有谁知道如何解决这个问题?

我目前正在考虑使用循环,str.replace('{a}', 1)但这也感觉很笨拙。

python

0
推荐指数
1
解决办法
955
查看次数

Python 多重继承 super().__init__()

我有两个类,它们的方法初始化了相同的参数__init__。我想继承“X”类中的两个类。但我会得到:TypeError: B.__init__() missing 1 required positional argument: 'my_param'

可重现的示例:

class A:
    def __init__(self, my_param):
        super().__init__()
        self.my_param = my_param


class B:
    def __init__(self, my_param):
        super().__init__()
        self.my_param = my_param * 2


class X(A, B):
    def __init__(self, my_param):
        super().__init__(my_param=my_param)

a = X(my_param=1)
print(a.my_param)
Run Code Online (Sandbox Code Playgroud)

A 和 B 是 Mixins,它们为子类提供附加功能。它们可以单独或一起使用。假设 A 类提供按 ID 搜索的 searchhc 功能,而 B 类提供按值搜索的功能。

有没有办法为 A 和 B 分别设置 my_param 或设置它而不会出现错误?

python inheritance multiple-inheritance

-2
推荐指数
1
解决办法
4947
查看次数