我有一个大约 7k 行和 4 列的文件。很多单元格都是空的,我尝试使用许多 pandas 函数删除它们,但似乎没有任何效果。我尝试过的功能和代码如下:
我尝试过的:
df = df.dropna(thresh=2)
and
df.dropna(axis=0, how='all')
My code:
file = "pc-dirty-data.csv" path = root + file name_cols = ['GUID1', 'GUID2', 'Record ID', 'Name', 'Org Name', 'Title'] pull_cols = ['Record ID', 'Name', 'Org Name', 'Title'] df = df.dropna(thresh=2) df.dropna(axis=0, how='all') df = pd.read_csv(path, header=None, encoding="ISO-8859-1", names=name_cols, usecols=pull_cols, index_col=False) df.info()
数据框:
RangeIndex: 6599 entries, 0 to 6598 Data columns (total 4 columns): Record ID 5874 non-null float64 Name 5874 non-null object Org Name 5852 non-null object Title 5615 non-null object dtypes: float64(1), object(3)
dropna不是就地操作,您需要将其重新分配回变量或使用inplace参数设置为 True。
dropna
inplace
df = df.dropna(axis=0, how='all')
or
df.dropna(axis=0, how='all', inplace=True)
Jay 在评论中指出,您需要重新排序代码逻辑,以便在 read_csv 之后删除。