來自https://www.cnblogs.com/guxh/p/9420610.html, 感謝作者。
創建df:
1
2
3
4
5
6
7
|
>>> df
=
pd.DataFrame(np.arange(
16
).reshape(
4
,
4
), columns
=
list
(
'ABCD'
), index
=
list
(
'1234'
))
>>> df
A B C D
1
0
1
2
3
2
4
5
6
7
3
8
9
10
11
4
12
13
14
15
|
1,刪除行
1.1,drop
通過行名稱刪除:
1
2
|
df
=
df.drop([
'1'
,
'2'
])
# 不指定axis默認為0
df.drop([
'1'
,
'3'
], inplace
=
True
)
|
通過行號刪除:
1
2
3
|
df.drop(df.index[
0
], inplace
=
True
)
# 刪除第1行
df.drop(df.index[
0
:
3
], inplace
=
True
)
# 刪除前3行
df.drop(df.index[[
0
,
2
]], inplace
=
True
)
# 刪除第1第3行
|
1.2,通過各種篩選方法實現刪除行
詳見pandas“選擇行單元格,選擇行列“的筆記
舉例,通過篩選可以實現很多功能,例如要對某行數據去重,可以獲取去重后的index列表后,使用loc方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> df.loc[
'2'
,
'B'
]
=
9
>>> df
A B C D
1
0
1
2
3
2
4
9
6
7
3
8
9
10
11
4
12
13
14
15
>>> chooses
=
df[
'B'
].drop_duplicates().index
>>> df.loc[chooses]
A B C D
1
0
1
2
3
2
4
9
6
7
4
12
13
14
15
|
2,刪除列
2.1,del
1
|
del
df[
'A'
]
# 刪除A列,會就地修改
|
2.2,drop
通過列名稱刪除:
1
2
|
df
=
df.drop([
'B'
,
'C'
], axis
=
1
)
# drop不會就地修改,創建副本返回
df.drop([
'B'
,
'C'
], axis
=
1
, inplace
=
True
)
# inplace=True會就地修改
|
使用列數刪除,傳入參數是int,列表,者切片:
1
2
3
|
df.drop(df.columns[
0
], axis
=
1
, inplace
=
True
)
# 刪除第1列
df.drop(df.columns[
0
:
3
], axis
=
1
, inplace
=
True
)
# 刪除前3列
df.drop(df.columns[[
0
,
2
]], axis
=
1
, inplace
=
True
)
# 刪除第1第3列
|
2.3,通過各種篩選方法實現刪除列
詳見pandas“選擇行單元格,選擇行列“的筆記
3.1,loc,at,set_value
想增加一行,行名稱為‘5’,內容為[16, 17, 18, 19]
1
2
3
|
df.loc[
'5'
]
=
[
16
,
17
,
18
,
19
]
# 后面的序列是Iterable就行
df.at[
'5'
]
=
[
16
,
17
,
18
,
19
]
df.set_value(
'5'
, df.columns, [
16
,
17
,
18
,
19
], takeable
=
False
)
# warning,set_value會被取消
|
3.2,append
添加有name的Series:
1
2
|
s
=
pd.Series([
16
,
17
,
18
,
19
], index
=
df.columns, name
=
'5'
)
df
=
df.append(s)
|
添加沒有name的Series,必須ignore_index:
1
2
|
s
=
pd.Series([
16
,
17
,
18
,
19
], index
=
df.columns)
df
=
df.append(s, ignore_index
=
True
)
|
可以 append字典列表,同樣需要必須ignore_index:
1
2
|
ls
=
[{
'A'
:
16
,
'B'
:
17
,
'C'
:
18
,
'D'
:
19
}, {
'A'
:
20
,
'B'
:
21
,
'C'
:
22
,
'D'
:
23
}]
df
=
df.append(ls, ignore_index
=
True
)
|
3.3,逐行增加
簡單的逐行添加內容,可以:
1
|
df.loc[
len
(df)]
=
[
16
,
17
,
18
,
19
]
|
但需要注意:len(df)生成的是int,如果生成的int,df已經存在了,會覆蓋該行數據,而不會新增
3.4,插入行
增加行沒找到類似insert這種可以插入的方法,暫時替代方法可以先reindex,再賦值:
1
2
|
df
=
df.reindex(index
=
df.index.insert(
2
,
'5'
))
df.loc[
'5'
]
=
[
16
,
17
,
18
,
19
]
|
一般涉及到增加列項時,經常會對現有的數據進行遍歷運算,獲得新增列項的值,所以這里結合對DataFrame的遍歷討論增加列。
例如,想增加一列'E',值等於'A'和'C'列對應值之和。
4.1,遍歷DataFrame獲取序列的方法
1
2
3
4
5
|
s
=
[a
+
c
for
a, c
in
zip
(df[
'A'
], df[
'C'
])]
# 通過遍歷獲取序列
s
=
[row[
'A'
]
+
row[
'C'
]
for
i, row
in
df.iterrows()]
# 通過iterrows()獲取序列,s為list
s
=
df.
apply
(
lambda
row: row[
'A'
]
+
row[
'C'
], axis
=
1
)
# 通過apply獲取序列,s為Series
s
=
df[
'A'
]
+
df[
'C'
]
# 通過Series矢量相加獲取序列
s
=
df[
'A'
].values
+
df[
'C'
].values
# 通過Numpy矢量相加獲取序列
|
4.2,[ ],loc
通過df[]或者df.loc添加序列
1
2
|
df.loc[:,
'E'
]
=
s
df[
'E'
]
=
s
|
4.3,Insert
可以指定插入位置,和插入列名稱
1
|
df.insert(
0
,
'E'
, s)
|
4.4,concat
1
2
|
s
=
pd.Series([
16
,
17
,
18
,
19
], name
=
'E'
, index
=
df.index)
df
=
pd.concat([df, s], axis
=
1
)
|
4.5,iloc和loc遍歷過程中給列賦值
效率比較低
df['E']是DataFrame的一個Series,是引用,對其修改也能改變DataFrame,但運行時報了Warning
1
2
3
4
|
df[
'E'
]
=
None
# 需事先創建e列,否則iloc遍歷會報錯,loc遍歷無需事先創建
for
i
in
range
(
len
(df)):
df[
'E'
].iloc[i]
=
df[
'A'
].iloc[i]
+
df[
'C'
].iloc[i]
# SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
|
不用Series不會報Warning:
1
2
3
4
|
df[
'E'
]
=
None
col_no
=
[i
for
i
in
df.columns].index(
'E'
)
for
i
in
range
(
len
(df)):
df.iloc[i, col_no]
=
df[
'A'
].iloc[i]
+
df[
'C'
].iloc[i]
|
用loc無需先給E列賦空值:
1
2
|
for
i
in
df.index:
df.loc[i,
'E'
]
=
df.loc[i,
'A'
]
+
df.loc[i,
'C'
]
|
4.6,逐列增加
簡單的逐列添加內容,可以:
1
|
df[
len
(df)]
=
[
16
,
17
,
18
,
19
]
|
但需要注意:len(df)生成的是int,如果生成的int,df已經存在了,會覆蓋該列數據,而不會新增
4.7,其他方法
增加3列,EFG,value默認為np.NaN
1
2
|
df
=
pd.concat([df, pd.DataFrame(columns
=
list
(
'EFG'
))])
# 列的次序無法指定,並且fillna時會對整個df做出調整
df
=
df.reindex(columns
=
list
(
'ABCDEFG'
), fill_value
=
0
)
# 列的次序按照list指定,並且fill_value只對新增列做出調整,推薦!
|