從 Pandas DataFrame 系列中獲取列表

Luqman Khan 2022年5月16日
從 Pandas DataFrame 系列中獲取列表

Python 是一種眾所周知的資料分析語言,主要歸功於 Python 包。Pandas 是幫助我們更輕鬆地分析資料的軟體包之一。

Pandas tolist() 方法將系列轉換為 Python 的系列或內建列表。預設情況下,series 是 pandas.core.series.Series 資料型別和 tolist() 方法的型別,轉換為資料列表。

使用 tolist() 方法從 Pandas DataFrame 系列中獲取列表

本文將討論如何從 Pandas Dataframe 列中獲取列表。我們將首先將 CSV 檔案讀入 Pandas DataFrame。

import pandas as pd
# read csv file
df=pd.read_csv('home_price.csv')
# display 3 rows 
df=df.head(3)
print(df)

輸出:

   Area  Home price
0  1000       10000
1  1200       12000
2  1300       13000

現在我們將從列中提取值並將其轉換為列表,因為我們知道 tolist() 有幫助。

list1=df['Home price'].values.tolist()
print('extract the value of series and converting into the list')
print(list1)

輸出:

extract the value of series and converting into the list
[10000, 12000, 13000, 14000, 15000]

列表是一個有序且靈活的 Python 容器,是 Python 中最常見的資料結構之一。元素被插入方括號 [],用逗號分隔以建立一個列表。列表可以包含重複值;這就是我們主要在資料集中使用列表的原因。

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)

在這種情況下,當前價格會增加 1.5 稅。現在我們建立一個名為 updated 的列表並更新現有列;此外,我們使用 to_csv() 方法建立一個新的 CSV 檔案。

輸出:

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

讓我們考慮另一個簡單的例子:

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))

請注意,系列資料型別被 tolist() 改變了,我們得到了一個包含 Dataframe 所有列的列表。

輸出:

    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'>

所有的程式碼都在這裡。

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))

輸出:

   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'>

相關文章 - Pandas DataFrame