小编A H*_*A H的帖子

为什么复制Pandas DataFrame后属性会丢失

为什么不能通过副本传递实例的属性?我想将该name属性传递给另一个数据帧.

import copy
df = pd.DataFrame([1,2,3])
df.name = 'sheet1'
df2 = copy.deepcopy(df)

print(f'df.name: {df.name}')
>> df.name: sheet1

print(f'df2.name: {df2.name}')
>>    AttributeError    
        ...      
      'DataFrame' object has no attribute 'name'
Run Code Online (Sandbox Code Playgroud)

同样,在创建类并从中继承时,为什么这也不起作用?

class DfWithName(pd.DataFrame):

    def __init__(self, *args, **kwargs):
        self.__init__ = super().__init__(*args, **kwargs)
        print('lol')

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value
Run Code Online (Sandbox Code Playgroud)

并使用相同的代码:

import copy
df = DfWithName([1,2,3])
df.name = 'sheet1'
df2 = copy.deepcopy(df) 
print(f'df.name: {df2.name}')
>>    AttributeError    
        ...      
      'DataFrame' object has no attribute 'name'
Run Code Online (Sandbox Code Playgroud)

python

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

tox多次测试,重用tox环境

是否可以使用单个 tox 虚拟环境执行以下操作?

[tox]
envlist = test, pylint, flake8, mypy
skipsdist = true

[testenv:lint]
deps = pylint
commands = pylint .

[testenv:flake8]
deps = flake8
commands = flake8 .

[testenv:mypy]
commands = mypy . --strict

[testenv:test]
deps = pytest
commands = pytest


Run Code Online (Sandbox Code Playgroud)

由于我只在我的 python 版本 (py3.7) 上进行测试,因此我不希望 tox 必须创建 4 个环境(.tox/test.tox/pylint.tox/flake8.tox/mypy),因为它们都可以在单个环境上运行。

我还想查看单独失败的内容,因此不想这样做:

[tox]
skipsdist = true

[testenv]
commands = pylint .
           flake8 .
           mypy . --strict
           pytest
Run Code Online (Sandbox Code Playgroud)

因为输出会是这样的:

_____________ summary …
Run Code Online (Sandbox Code Playgroud)

python tox

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

Pandas Loc 在单个表达式中按索引和布尔条件选择

我有一个简化的数据框,可以按如下方式设置:

indexes =['01/10/2017', '28/10/2018', '27/10/2019', '30/10/2019']
cols = ['Period', 'A', 'B', 'C']
df= pd.DataFrame(index = indexes, columns= cols)
df.Period = 1
df = pd.concat([df, 2*df.copy(), 3*df.copy()])
df.sort_index()
Run Code Online (Sandbox Code Playgroud)

数据框看起来像:

    Period        A      B       C
01/10/2017  1   NaN     NaN     NaN
01/10/2017  2   NaN     NaN     NaN
01/10/2017  3   NaN     NaN     NaN
27/10/2019  1   NaN     NaN     NaN
27/10/2019  2   NaN     NaN     NaN
27/10/2019  3   NaN     NaN     NaN
28/10/2018  1   NaN     NaN     NaN
28/10/2018  2   NaN     NaN     NaN
28/10/2018  3   NaN     NaN     NaN
30/10/2019  1   NaN …
Run Code Online (Sandbox Code Playgroud)

python pandas

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

大熊猫转移行NaNs

假设我们的数据框设置如下:

x = pd.DataFrame(np.random.randint(1, 10, 30).reshape(5,6),
                 columns=[f'col{i}' for i in range(6)])
x['col6'] = np.nan
x['col7'] = np.nan

    col0    col1    col2    col3    col4    col5    col6    col7
 0   6       5        1       5       2       4      NaN    NaN
 1   8       8        9       6       7       2      NaN    NaN
 2   8       3        9       6       6       6      NaN    NaN
 3   8       4        4       4       8       9      NaN    NaN
 4   5       3        4       3       8       7      NaN    NaN     
Run Code Online (Sandbox Code Playgroud)

拨打电话时x.shift(2, axis=1),col2 -> col5移动正确的,但col6并 …

python pandas

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

检查整个 pandas 对象列是否是字符串

即使数据类型是对象,如何检查列是否是字符串或其他类型(例如 int 或 float)?

(理想情况下,我希望此操作矢量化,而不是applymap检查每一行......)

import io
# American post code
df1_str = """id,postal
1,12345
2,90210
3,"""
df1 = pd.read_csv(io.StringIO(df1_str))
df1["postal"] = df1["postal"].astype("O")  # is an object (of type float due to the null row 3)
Run Code Online (Sandbox Code Playgroud)
# British post codes
df2_str = """id,postal
1,EC1
2,SE1
3,W2"""
df2 = pd.read_csv(io.StringIO(df2_str))
df2["postal"] = df2["postal"].astype("O")  # is an object (of type string)
Run Code Online (Sandbox Code Playgroud)

执行时两者df1df2返回objectdf["postal"].dtype

  • 但是,df2.str方法,例如df2["postal"].str.lower(),但df1没有。
  • 类似地,df1可以对其进行数学运算,例如 …

python pandas

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

使用select语句模拟表

类似的问题,以模拟表具有多个行只是SELECT语句 -这个问题问的一个单列多行的.

如何模拟具有多个列和行的表?

我已经做到这一点(多列单行):

SELECT 'John Doe' AS [Customer Name],
       '31' AS [Customer Age],
       'pizza' AS [Food]
Run Code Online (Sandbox Code Playgroud)

但不确定如何获取多行数据.

使用sql-server

sql sql-server select

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

标签 统计

python ×5

pandas ×3

select ×1

sql ×1

sql-server ×1

tox ×1