Python是進行數據分析的一種出色語言,主要是因為以數據為中心的Python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas tolist()用於將係列轉換為列表。最初,該係列的類型為pandas.core.series.Series,並應用tolist()方法,將其轉換為列表數據類型。
用法:Series.tolist()
返回類型:轉換成列表
要下載以下示例中使用的數據集,請單擊此處。在以下示例中,使用的 DataFrame 包含一些NBA球員的數據。下麵是任何操作之前的數據幀圖像。
例:
在此示例中,“名稱”列的數據類型存儲在變量中。之後,使用tolist()方法進行轉換,然後再次存儲和打印數據類型。
# importing pandas module
import pandas as pd
# importing regex module
import re
# making data frame
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")
# removing null values to avoid errors
data.dropna(inplace = True)
# storing dtype before operation
dtype_before = type(data["Salary"])
# converting to list
salary_list = data["Salary"].tolist()
# storing dtype after operation
dtype_after = type(salary_list)
# printing dtype
print("Data type before converting = {}\nData type after converting = {}".format(dtype_before, dtype_after))
# displaying list
salary_list
輸出:
如輸出圖像中所示,數據類型從Series轉換為List。 salary_list的輸出為列表格式。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Kartikaybhutani大神的英文原創作品 Python | Pandas Series.tolist()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。