dropna(axis=0, how=’any’, thresh=None, subset=None, inplace=False)
axis{0 or ‘index’, 1 or ‘columns’}, default 0
-
0, or ‘index’ : Drop rows which contain missing values.
-
1, or ‘columns’ : Drop columns which contain missing value.
how{‘any’, ‘all’}, default ‘any’
-
‘any’ : If any NA values are present, drop that row or column.
- ‘any’: 只要有一個NA,就整列(欄)刪除
-
‘all’ : If all values are NA, drop that row or column.
- ‘all’: 全部都NA,才刪除該列(欄)
thresh : int, optional
Require that many non-NA values. Cannot be combined with how.
(threshold 門檻)
非空元素最低数量。int型,默认为None。
如果该行/列中,非空元素数量小于这个值,就删除该行/列。
subset column label or sequence of labels, optional
Labels along other axis to consider, e.g. if you are dropping rows these would be a list of columns to include.
inplace bool, default False
Whether to modify the DataFrame rather than creating a new one.
inplace:是否原地替换。布尔值,默认为False
直接將csv檔讀進來DataFrame:

原DataFrame有
[13 rows x 7 columns]
其中2 rows , 1 column 為空(NaN)
程式碼:
import pandas as pd
fpath = r”C:\antenna_AMS\21046\emt2csv\01\01test.csv”
df = pd.read_csv(fpath)
#df = pd.read_csv(fpath,header=None) ;
# 讀取無欄標籤的檔案,自動加0,1,2…當欄標籤
df_drop0 = df.dropna(axis=0, how=’all’)
#先drop空列,axis=0
df_drop1 = df_drop0.dropna(axis=1,how=’all’)
#再drop空欄,axis=1

df_drop0 = df.dropna(axis=0, how=’all’)
[11 rows x 7 columns]
刪除了兩空列
df_drop1 = df_drop0.dropna(axis=1,how=’all’)
對df_drop0再drop空欄(axis=1)

[11 rows x 6 columns]
刪除了一個空欄
刪除空列會導致index不連續
推薦hahow線上學習python: https://igrape.net/30afN
![Python: 如何創建多層column name的pandas.DataFrame? df = pd.read_csv (‘data.csv’, header=[0, 1], sep=”,”) ; col = pd .MultiIndex .from_arrays( aryCol ) Python: 如何創建多層column name的pandas.DataFrame? df = pd.read_csv (‘data.csv’, header=[0, 1], sep=”,”) ; col = pd .MultiIndex .from_arrays( aryCol )](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230314164119_32.png?quality=90&zoom=2&ssl=1&resize=350%2C233)




![Python: 如何使用functools.reduce逐步縮減可迭代對象,合併為單個結果? import functools; product = functools.reduce( lambda x, y: x * y, numbers) ; reduce(function, sequence [, initial]) -> value ; map(function, iterable) ; filter(function, iterable) ; map ; filter Python: 如何使用functools.reduce逐步縮減可迭代對象,合併為單個結果? import functools; product = functools.reduce( lambda x, y: x * y, numbers) ; reduce(function, sequence [, initial]) -> value ; map(function, iterable) ; filter(function, iterable) ; map ; filter](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/06/20230626093403_49.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: matplotlib如何設定座標軸刻度? plt.xticks(seq, labels) ;如何生成fig, ax物件? fig = plt.figure(figsize= (10.24, 7.68)) ; ax = fig.add_subplot() ; fig, ax = plt.subplots(figsize=(10.24, 7.68)) ; 如何使用中文? plt.rcParams[“font.family”] = [“Microsoft JhengHei”] Python: matplotlib如何設定座標軸刻度? plt.xticks(seq, labels) ;如何生成fig, ax物件? fig = plt.figure(figsize= (10.24, 7.68)) ; ax = fig.add_subplot() ; fig, ax = plt.subplots(figsize=(10.24, 7.68)) ; 如何使用中文? plt.rcParams[“font.family”] = [“Microsoft JhengHei”]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230209083006_41.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


近期留言