English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Pandas-Serie

Basisoperationen von Pandas Series

pandas.Series

Die Struktur der Series ist wie folgt:

pandas.Series(data, index, dtype, copy)

The parameters of the constructor are as follows-

data: Data can take various forms, such as ndarray, list, constant index: The index values must be unique and hashable, and the length must be the same as the data. If no index is passed, the default is np.arange(n). dtype: dtype is used for data type. If None, the data type will be inferred copy: Copy data. Default is False

Series can be created from various inputs, such as

Array Dict Scalar value or constant

Create an empty Series

 >>> # Import the pandas dependency package and create an alias
 >>> import pandas as pd
 >>> s = pd.Series()
 >>> print(s)
 Series([], dtype: float64)

Create a Series from an ndarray

If the data is an ndarray, the passed index must have the same length. If no index is passed, the default index will be range(n), where n is the length of the array, i.e. [0,1,2,3…。范围(len(array))-1]。

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 import numpy as np
 data = np.array(['a','b','c','d'])
 s = pd.Series(data)
 print(s)

Laufende Ergebnisse:

 0 a
 1 b
 2 c
 3 d
 dtype: object

We did not pass any index, so by default, it assigns the index range from 0 to len(data)-1,即0到3.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 import numpy as np
 data = np.array(['a','b','c','d'])
 s = pd.Series(data,index=[100,101,102,103])
 print(s)

Laufende Ergebnisse:

 100 a
 101 b
 102 c
 103 d
 dtype: object

We passed the index values here. Now, we can see the custom index values in the output.

Create a Series from a dictionary

A dictionary can be passed as input. If no index is specified, all the dictionary keys are taken in sorted order to build the index. If an index is passed, the values corresponding to the index labels will be pulled out.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 import numpy as np
 data = {'a' : 0., 'b' : 1., 'c' : 2.}
 s = pd.Series(data)
 print(s)

Laufende Ergebnisse:

 a 0.0
 b 1.0
 c 2.0
 dtype: float64

Dictionary keys are used to construct the index.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 import numpy as np
 data = {

Laufende Ergebnisse:

 b 1.0
 c 2.0
 d NaN
 a 0.0
 dtype: float64

The order of the index is maintained, and missing elements are filled with NaN (not a number).

Create a Series from a scalar

If the data is a scalar value, an index must be provided. This value will be repeated to match the length of the index

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 import numpy as np
 s = pd.Series(5, index=[0, 1, 2, 3])
 print(s)

Laufende Ergebnisse:

 
 0 5
 1 5
 2 5
 3 5
 dtype: int64

Access data from a Series with a location

Data in Series can be accessed as if it were an ndarray.
Suchen des ersten Elements. Es ist bekannt, dass die Zählung der Arrays von Null beginnt, was bedeutet, dass der erste Element im Nullten Ort gespeichert ist, und so weiter.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 s = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
 # Suchen des ersten Daten
 print s[0]

Laufende Ergebnisse:

1

Suchen der ersten drei Elemente der Series. Wenn vorne eingefügt wird, werden alle Projekte ab diesem Index extrahiert. Wenn zwei Parameter (zwischen ihnen mit :) verwendet werden, werden die Projekte zwischen den beiden Indizes (ausgenommen der Stoppindex) abgerufen

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 s = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
 # Suchen der ersten3Elemente
 print s[:3]

Laufende Ergebnisse:

 a 1
 b 2
 c 3
 dtype: int64

Suchen der letzten drei Elemente.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 s = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
 # Suchen der letzten drei Elemente
 print s[-3:]

Laufende Ergebnisse:

 c 3
 d 4
 e 5
 dtype: int64

Daten mit Beschriftungen (Index) abrufen

Eine Series ähnelt einem festgrößenmäßigen Wörterbuch und kann Werte über Indexbeschriftungen abrufen und festlegen.
Suchen eines einzelnen Elements mit der Wertbeschriftung des Index.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 s = pd.Series([1,2,3,4,5],index=[

Laufende Ergebnisse:

 1

Suchen mehrerer Elemente mit der Werteliste der Indexbeschriftung.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 s = pd.Series([1,2,3,4,5],index=[

Laufende Ergebnisse:

 
 a 1
 c 3
 d 4
 dtype: int64

Falls keine Beschriftungen enthalten sind, wird eine Ausnahme ausgelöst.

 # Dateiname: pandas.py
 # Autor von: de.oldtoolbag.com 
 # Importieren Sie das Abhängigkeitspaket pandas und benennen Sie es um
 import pandas as pd
 s = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
 # Suchen mehrerer Elemente
 print(s['f'])

Laufende Ergebnisse:

   …
 KeyError: 'f'