【问题标题】:How to clean CSV file for a coordinate system using pandas?如何使用熊猫清理坐标系的 CSV 文件?
【发布时间】:2021-03-05 16:08:45
【问题描述】:

我想创建一个程序来将CSV文件转换为DXF(AutoCAD),但是CSV文件有时带有标题有时没有,并且有不能为空的单元格,例如坐标,我还注意到排除后一些输入值是 nan 或 NaN 并且有必要摆脱它们,所以我为您提供我的答案,并请分享您的意见以实施更好的方法。

样本输入

输出

【问题讨论】:

    标签: python-3.x pandas csv coordinates ezdxf


    【解决方案1】:

    解决方案

    import string
    import pandas
    
    
    def pandas_clean_csv(csv_file):
        """
        Function pandas_clean_csv Documentation
        - I Got help from this site, it's may help you as well:
        Get the row with the largest number of missing data for more Documentation
        https://moonbooks.org/Articles/How-to-filter-missing-data-NAN-or-NULL-values-in-a-pandas-DataFrame-/
        """
        try:
            if not csv_file.endswith('.csv'):
                raise TypeError("Be sure you select .csv file")
            
            # get punctuations marks as list !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
            punctuations_list = [mark for mark in string.punctuation]
        
            # import csv file and read it by pandas
            data_frame = pandas.read_csv(
                filepath_or_buffer=csv_file,
                header=None,
                skip_blank_lines=True,
                error_bad_lines=True,
                encoding='utf8',
                na_values=punctuations_list
            )
            
            # if elevation column is NaN convert it to 0
            data_frame[3] = data_frame.iloc[:, [3]].fillna(0)
            
            # if Description column is NaN convert it to -
            data_frame[4] = data_frame.iloc[:, [4]].fillna('-')
            
            # select coordinates columns
            coord_columns = data_frame.iloc[:, [1, 2]]
            
            # convert coordinates columns to numeric type
            coord_columns = coord_columns.apply(pandas.to_numeric, errors='coerce', axis=1)
            
            # Find rows with missing data
            index_with_nan = coord_columns.index[coord_columns.isnull().any(axis=1)]
            
            # Remove rows with missing data
            data_frame.drop(index_with_nan, 0, inplace=True)
            
            # iterate data frame as tuple data
            output_clean_csv = data_frame.itertuples(index=False)
            
            return output_clean_csv
        
        except Exception as E:
            print(f"Error: {E}")
            exit(1)
    
    
    out_data = pandas_clean_csv('csv_files/version2_bad_headers.csl')
    
    for i in out_data:
        print(i[0], i[1], i[2], i[3], i[4])
    

    这里你可以Download my test CSV文件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 2019-07-26
      • 2019-10-10
      • 1970-01-01
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多