提取需要的数据

import pandas as pd
import os
from common.common_tool import make_dir_if_not_exists
from conf.path_config import data_dir


# 获取所有数据,并利用字典封装
def get_data_dict(path1, path2):
    df_t1 = pd.read_csv(path1)
    drop_gid = [10011763, 100100009, 100100010, 100100011, 100100012, 100100015, 200287136, 200305058]
    df_t1.drop(df_t1[df_t1['node_code'].isin(drop_gid)].index, inplace=True)
    df_t1 = dict(zip(df_t1['node_code'], df_t1['mark']))
    for i, j in df_t1.items():
        print(i)
        print(j)
        data = pd.read_csv(path2 + '/' + str(i) + '.csv')
        if j == 'p':
            data = data[['ts', 'pressure']]
            mean_data = data['pressure'].mean()
            data.loc[data['pressure'] == 0, 'pressure'] = mean_data
            data.loc[data['pressure'].isnull(), 'pressure'] = mean_data
        else:
            data = data[['ts', 'flow']]
            mean_data = data['flow'].mean()
            data.loc[data['flow'] == 0, 'flow'] = mean_data
            data.loc[data['flow'].isnull(), 'flow'] = mean_data
		
		new_col = ['ts', 'data']
        data.columns = new_col

        make_dir_if_not_exists(os.path.join(data_dir, '54857', str(i)))
        data.to_csv(os.path.join(data_dir, '54857', str(i),  str(i) + ".csv"), index=False)


if __name__ == '__main__':
    # 获取所有数据,并利用字典封装
    get_data_dict("dt_qd_mark.csv", "../../data/origin_data")

你可能感兴趣的:(python数据处理,python,数学建模,开发语言)