Python Pandas 對列/行 Column/Row 進行選擇,增加,刪除操作

一、列操作

1.1 選擇列

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
   'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print (df ['one'])
# 選擇其中一列進行顯示,列長度爲最長列的長度
# 除了 index 和 數據,還會顯示 列表頭名,和 數據 類型

運行結果:

a    1.0
b    2.0
c    3.0
d    NaN
Name: one, dtype: float64

1.2 增加列

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
   'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)

# Adding a new column to an existing DataFrame object with column label by passing new series

print ("Adding a new column by passing as Series:")
df['three']=pd.Series([10,30,20],index=['a','c','b'])
print(df)
# 增加列後進行顯示,其中 index 用於對應到該列 元素 位置(所以位置可以不由 列表 中的順序進行指定)

print ("Adding a new column using the existing columns in DataFrame:")
df['four']=df['one']+df['two']+df['three']
print(df)
# 我們選定列後,直接可以對整個列的元素進行批量運算操作,這裏 NaN 與其他元素相加後,還是 NaN

運行結果:

Adding a new column by passing as Series:
   one  two  three
a  1.0    1   10.0
b  2.0    2   20.0
c  3.0    3   30.0
d  NaN    4    NaN
Adding a new column using the existing columns in DataFrame:
   one  two  three  four
a  1.0    1   10.0  12.0
b  2.0    2   20.0  24.0
c  3.0    3   30.0  36.0
d  NaN    4    NaN   NaN

1.3 刪除列(del 和 pop 函數)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
   'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']),
   'three' : pd.Series([10,20,30], index=['a','b','c'])}

df = pd.DataFrame(d)
print ("Our dataframe is:")
print(df)

# 使用 del 函數
print ("Deleting the first column using DEL function:")
del(df['one'])
print(df)

# 使用 pop 函數
print ("Deleting another column using POP function:")
df_2=df.pop('two') # 將一列 pop 到新的 dataframe
print(df_2)
print(df)

運行結果:

Our dataframe is:
   one  two  three
a  1.0    1   10.0
b  2.0    2   20.0
c  3.0    3   30.0
d  NaN    4    NaN
Deleting the first column using DEL function:
   two  three
a    1   10.0
b    2   20.0
c    3   30.0
d    4    NaN
Deleting another column using POP function:
   three
a   10.0
b   20.0
c   30.0
d    NaN
POP column:
a    1
b    2
c    3
d    4
Name: two, dtype: int64

二、行操作

2.1 選擇行

2.1.1 通過 label 選擇行(loc 函數)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
   'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df.loc['b']) # 顯示這一行中,對應表頭 下的 對應數據,同時顯示 行 index 和 數據類型

運行結果:

one    2.0
two    2.0
Name: b, dtype: float64

2.1.2 通過序號選擇行(iloc 函數)

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
   'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df.iloc[2]) # 序號 2 對應的是第 3 行的數據

運行結果:

one    3.0
two    3.0
Name: c, dtype: float64

2.1.3 通過序號選擇行切片

d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
   'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

df = pd.DataFrame(d)
print(df[2:4]) # 這裏選擇第 3 到 第 4 行,與 Python 切片一致,不需要函數,直接切片即可

運行結果:

   one  two
c  3.0    3
d  NaN    4

2.2 增加行(append 函數)

# 通過 append 函數
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)
print(df) # 這裏相當於把 第二個 dataframe 與第一個進行拼接,默認的 index 都是 0 1
print(df.loc[0]) # 這裏有兩行的 index 是 0

運行結果:

   a  b
0  1  2
1  3  4
0  5  6
1  7  8
   a  b
0  1  2
0  5  6

2.3 刪除行(drop 函數)

# 通過 drop 函數
df = pd.DataFrame([[1, 2], [3, 4]], columns = ['a','b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns = ['a','b'])

df = df.append(df2)

df = df.drop(0) # 這裏有兩個行標籤爲 0,所以直接刪除了 2 行
print(df)

運行結果:

   a  b
1  3  4
1  7  8
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章