Holen Sie sich eine Liste aus der Pandas DataFrame-Serie
Python ist eine bekannte Sprache für die Datenanalyse, hauptsächlich aufgrund der Python-Pakete. Pandas ist eines dieser Pakete, die uns helfen, Daten viel einfacher zu analysieren.
Pandas tolist()-Methode konvertiert eine Serie in eine Serie oder eine eingebaute Liste von Python. Standardmäßig ist die Serie vom Datentyp pandas.core.series.Series und der Methode tolist(), konvertiert in eine Liste von Daten.
Verwenden Sie die Methode tolist(), um eine Liste aus der Pandas DataFrame-Serie abzurufen
In diesem Artikel wird erläutert, wie Sie eine Liste aus der Pandas Dataframe-Spalte erhalten. Wir werden zuerst eine CSV-Datei in einen Pandas DataFrame einlesen.
import pandas as pd
# read csv file
df = pd.read_csv("home_price.csv")
# display 3 rows
df = df.head(3)
print(df)
Ausgabe:
Area Home price
0 1000 10000
1 1200 12000
2 1300 13000
Jetzt extrahieren wir den Wert aus der Spalte und wandeln ihn in die Liste um, da wir wissen, dass tolist() hilft.
list1 = df["Home price"].values.tolist()
print("extract the value of series and converting into the list")
print(list1)
Ausgabe:
extract the value of series and converting into the list
[10000, 12000, 13000, 14000, 15000]
Die Liste ist ein geordneter und flexibler Python-Container, eine der häufigsten Datenstrukturen in Python. Elemente werden in eckige Klammern [] eingefügt, durch Kommas getrennt, um eine Liste zu erstellen. Die Liste kann doppelte Werte enthalten; Deshalb verwenden wir hauptsächlich Listen in Datensätzen.
import numpy as np
import pandas as pd
# read csv file
df = pd.read_csv("home_price.csv")
# extract the value of series and converting into the list
list1 = df["Home price"].values.tolist()
list1 = np.array(list1)
# type casting in list data type
updated = list(list1 * 1.5)
print("after include 1.5 % tax\n")
print(updated, "new home price")
df["Home price"] = updated
# create new csv
df.to_csv("home prices after 1 year.csv")
df2 = pd.read_csv("home prices after 1 year.csv")
print(df2)
In diesem Fall werden die Preise heutzutage um 1.5 Steuern erhöht. Jetzt erstellen wir eine Liste mit dem Namen updated list und aktualisieren die bestehende Spalte; Außerdem erstellen wir eine neue CSV-Datei mit der Methode to_csv().
Ausgabe:
after include 1.5 % tax
[15000.0, 18000.0, 19500.0, 21000.0, 22500.0] new home price
Unnamed: 0 Area Home price
0 0 1000 15000.0
1 1 1200 18000.0
2 2 1300 19500.0
3 3 1400 21000.0
4 4 1500 22500.0
Betrachten wir ein weiteres einfaches Beispiel:
import pandas as pd
df = pd.DataFrame(
{
"Country": ["Pakistan", "India", "America", "Russia", "China"],
"Immigrants": ["2000", "2500", "6000", "4000", "1000"],
"Years": ["2010", "2008", "2011", "2018", "2016"],
}
)
print(df, "\n")
list = df.columns.tolist()
print(type(df.columns))
print("\n", list, "\n")
print("After type cast into the list")
print(type(list))
Bitte beachten Sie, dass der Datentyp der Serie durch tolist() geändert wird und wir eine Liste mit allen Spalten von Dataframe erhalten.
Ausgabe:
Country Immigrants Years
0 Pakistan 2000 2010
1 India 2500 2008
2 America 6000 2011
3 Russia 4000 2018
4 China 1000 2016
<class 'pandas.core.indexes.base.Index'>
['Country', 'Immigrants', 'Years']
After type cast into the list
<class 'list'>
Alle Codes sind hier drin.
import numpy as np
import pandas as pd
# read csv file
df = pd.read_csv("home_price.csv")
# display 3 rows
df = df.head(3)
print(df)
list1 = df["Home price"].values.tolist()
print("extract the value of series and converting into the list")
print(list1)
# another example
# read csv file
df = pd.read_csv("home_price.csv")
# extract the value of series and converting into the list
list1 = df["Home price"].values.tolist()
list1 = np.array(list1)
# type casting in list data type
updated = list(list1 * 1.5)
print("after include 1.5 % tax\n")
print(updated, "new home price")
df["Home price"] = updated
# create new csv
df.to_csv("home prices after 1 year.csv")
df2 = pd.read_csv("home prices after 1 year.csv")
print(df2)
# another example
df = pd.DataFrame(
{
"Country": ["Pakistan", "India", "America", "Russia", "China"],
"Immigrants": ["2000", "2500", "6000", "4000", "1000"],
"Years": ["2010", "2008", "2011", "2018", "2016"],
}
)
print(df, "\n")
list = df.columns.tolist()
print(type(df.columns))
print("\n", list, "\n")
print("After type cast into the list")
print(type(list))
Ausgabe:
Area Home price
0 1000 10000
1 1200 12000
2 1300 13000
extract the value of series and converting into the list
[10000, 12000, 13000]
after include 1.5 % tax
[15000.0, 18000.0, 19500.0, 21000.0, 22500.0] new home price
Unnamed: 0 Area Home price
0 0 1000 15000.0
1 1 1200 18000.0
2 2 1300 19500.0
3 3 1400 21000.0
4 4 1500 22500.0
Country Immigrants Years
0 Pakistan 2000 2010
1 India 2500 2008
2 America 6000 2011
3 Russia 4000 2018
4 China 1000 2016
<class 'pandas.core.indexes.base.Index'>
['Country', 'Immigrants', 'Years']
After type cast into the list
<class 'list'>
Verwandter Artikel - Pandas DataFrame
- Wie man Pandas DataFrame-Spaltenüberschriften als Liste erhält
- Pandas DataFrame-Spalte löschen
- Wie man DataFrame-Spalte in Datetime in Pandas konvertiert
- Wie konvertiert man eine Fließkommazahl in eine Ganzzahl in Pandas DataFrame
- Wie man Pandas-DataFrame nach den Werten einer Spalte sortiert
- Wie erhält man das Aggregat der Pandas gruppenweise und sum