Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.python > #5715

Re: [Python-de] Verstaendnisproblem

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Mike Müller <mmueller@python-academy.de>
Newsgroups de.comp.lang.python
Subject Re: [Python-de] Verstaendnisproblem
Date Fri, 12 Feb 2021 21:32:46 +0100
Organization Python Academy GmbH & Co. KG
Lines 87
Message-ID <mailman.1117.1613162882.7193.python-de@python.org> (permalink)
References <2763401.e9J7NaK4W3@host.fritz.box> <107543700.nniJfEyVGO@host.fritz.box> <bff5cf57-b57b-300b-17d8-5390cd8bb3da@web.de> <5436893.DvuYhMxLoT@host.fritz.box> <8c5a698d-6083-1244-f205-c843af42ef02@sschwarzer.net> <0f1c0b5f-3aa9-9576-0ac2-b19483be4dfd@web.de> <2aedd0a8-32a5-a2ba-3bce-3b77dd108667@python-academy.de>
Reply-To mmueller@python-academy.de
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Trace news.uni-berlin.de BKYdwCaY3W5cNvWNlbmZXA3Xf5RiU0MCED5Yzvw6xrow==
Return-Path <mmueller@python-academy.de>
X-Original-To python-de@python.org
Delivered-To python-de@mail.python.org
Authentication-Results mail.python.org; dkim=none reason="no signature"; dkim-adsp=none (unprotected policy); dkim-atps=neutral
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:78.0) Gecko/20100101 Thunderbird/78.7.1
In-Reply-To <0f1c0b5f-3aa9-9576-0ac2-b19483be4dfd@web.de>
Content-Language de-DE
X-Authenticated-Sender mmueller@python-academy.de
X-Virus-Scanned Clear (ClamAV 0.102.4/26078/Fri Feb 12 13:15:26 2021)
X-BeenThere python-de@python.org
X-Mailman-Version 2.1.34
Precedence list
List-Id Die Deutsche Python Mailingliste <python-de.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-de>, <mailto:python-de-request@python.org?subject=unsubscribe>
List-Archive <https://mail.python.org/pipermail/python-de/>
List-Post <mailto:python-de@python.org>
List-Help <mailto:python-de-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-de>, <mailto:python-de-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <2aedd0a8-32a5-a2ba-3bce-3b77dd108667@python-academy.de>
X-Mailman-Original-References <2763401.e9J7NaK4W3@host.fritz.box> <107543700.nniJfEyVGO@host.fritz.box> <bff5cf57-b57b-300b-17d8-5390cd8bb3da@web.de> <5436893.DvuYhMxLoT@host.fritz.box> <8c5a698d-6083-1244-f205-c843af42ef02@sschwarzer.net> <0f1c0b5f-3aa9-9576-0ac2-b19483be4dfd@web.de>
Xref csiph.com de.comp.lang.python:5715

Show key headers only | View raw


Am 12.02.21 um 17:30 schrieb Peter Otten:
> On 12/02/2021 16:07, Stefan Schwarzer wrote:
>> On 2021-02-12 15:29, eilfh wrote:
>>> Am Donnerstag, 11. Februar 2021, 18:48:23 CET schrieb Peter Otten:
>>> Hallo, Peter
>>>
>>> danke für Deine Antwort.
>>>
>>> Das war das fehlende Puzzle in meinem Programm
>>>
>>>>   >>> x
>>>>
>>>> 0    4
>>>> Name: B, dtype: int64
>>>>
>>>>   >>> type(x)
>>>>
>>>> <class 'pandas.core.series.Series'>
>>>>
>>>>
>>>> kannst Du einfach entpacken
>>>>
>>>>   >>> [y] = x
>>>>   >>> y
>>>>
>>>> 4
>>>
>>>
>>> um ehrlich zu sein, ich versteh's zwar nicht,
>>> aber es läuft und erfüllte genau den Zweck
>>
>> Falls du `[y] = x` meinst, kann ich mir vorstellen, dass das
>> komisch aussieht. Das ist sogenanntes implizites "Tuple
>> unpacking", hier vielleicht eher "List unpacking". Du kannst
>> das auch als `y = x[0]` schreiben; ich denke, das ist klarer.
> 
> Wenn x vom Typ tuple oder list ist, gerne; aber pandas ist ziemlich komplex und 
> hält immer eine Überraschung bereit:
> 
>  >>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
>  >>> ser = df.loc[df.A == 1, "B"]
>  >>> ser[0]
> 4
> 
> Scheint zu funktionieren, ist aber Zufall:
> 
>  >>> ser = df.loc[df.A == 2, "B"]
>  >>> ser[0]
> Traceback (most recent call last):
> [...]
> KeyError: 0

Wenn du das erste Element möchtest solltest du den Zugriff "position based" [1]
machen:

 >>> ser.iloc[0]
5

Dagegen ist:

 >>> ser[0]

"label based"

und entspricht:

 >>> ser.loc[0]

Außerdem kann man nach `loc` und `iloc` besser googeln als nach `[y] = ser`. ;)

[1] 
https://pandas.pydata.org/docs/user_guide/indexing.html#different-choices-for-indexing

> 
> Mit Unpacking:
> 
>  >>> [y] = ser
>  >>> y
> 5
> 
> D. h. eine Series verhält sich beim Iterieren wie eine Liste, beim Lookup wie 
> ein dict.
> 
> _______________________________________________
> python-de maillist  -  python-de@python.org
> https://mail.python.org/mailman/listinfo/python-de

Back to de.comp.lang.python | Previous | Next | Find similar


Thread

Re: [Python-de] Verstaendnisproblem Mike Müller <mmueller@python-academy.de> - 2021-02-12 21:32 +0100

csiph-web