csv文件NA 空白的处理 2019-02-19

https://code.likeagirl.io/how-to-use-python-to-remove-or-modify-empty-values-in-a-csv-dataset-34426c816347

Solution 1: Replace empty/null values with a space

  • Fill all null or empty cells in your original DataFrame with an empty space and set that to a new DataFrame variable, here, called ‘modifiedFlights’*. modifiedFlights=flights.fillna(“ “)
  • Verify that you no longer have any null values by running modifiedFlights.isnull().sum()
  • Save your modified dataset to a new CSV, replacing ‘modifiedFlights.csv’ with whatever you would like to name your new file. modifiedFlights.to_csv('modifiedFlights.csv',index=False)
  • If you wish, you can replace your original DataFrame, using flights=flights.fillna(" ")

Solution 2: Remove rows with empty values

  • If there are only a few null values and you know that deleting values will not cause adverse effects on your result, remove them from your DataFrame and store that in a new DataFrame.
    modifiedFlights = flights.dropna()
  • Verify that you no longer have any null values by running modifiedFlights.isnull().sum()
  • Save your modified dataset to a new CSV, replacing ‘modifiedFlights.csv’ with whatever you would like to name your new file. modifiedFlights.to_csv('modifiedFlights.csv',index=False)
  • If you wish, you can replace your original DataFrame, using flights=flights.dropna()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。