import pandas as pd
import numpy as np
import netCDF4 as nc
# 读取txt文件
file = r'D:\Desktop\root\runoff\runoff.txt'
with open(file, 'r') as f:
data = pd.read_table(file, sep="\s+", header=None, names=["x", "y", "dat"], low_memory=False) # 读取txt并分隔
data = pd.DataFrame(data)
duplicate = data.duplicated(subset=['x', 'y'], keep=False) # 按"x","y"提取重复索引
duplicate_data = data.loc[duplicate, :]
duplicate_data['dat'] = duplicate_data['dat'].astype(float) # 将数据转化为浮点数,索引转换为整数类型
duplicate_data['x'] = duplicate_data['x'].astype(int)
duplicate_data['y'] = duplicate_data['y'].astype(int)
duplicate_data_sum = duplicate_data.groupby(by=['x', 'y'], as_index=False).agg({'dat': sum}) # 按相同索引将重复值求和
# print(dupl
提取txt中二维索引变量,并写入nc文件
最新推荐文章于 2023-03-28 10:25:58 发布