前面已经将32×32的数据删除了不需要的列,数据变成了32×21的数据
excel的粒径为了匹配txt的32行数据,我进行了重复复制,将excel变成下图:
那么采用数浓度公式:
代码:
# -*- coding:utf-8 -*- """ @author: SuYue @file: shunongdu.py @time: 2024/04/30 @desc: """ import numpy as np import pandas as pd df1 = pd.read_excel('D:/lianxi/直径.xls') # delta_d = {'变化直径':[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.188, 0.25, 0.25, 0.25, 0.25, 0.375, 0.5, 0.5, 0.5, 0.5, 0.75, 1, 1]} speed = {'速度':[0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95, 1.1, 1.3, 1.5, 1.7, 1.9, 2.2, 2.6, 3, 3.4, 3.8, 4.4, 5.2, 6, 6.8, 7.6, 8.8, 10.4, 12, 13.6, 15.2, 17.6, 20.8]} # df1 = pd.DataFrame(delta_d) df2 = pd.DataFrame(speed) file_path = 'D:/lianxi/53469-20220718_out.txt' # 读整个txt文件读取到单个字符串 with open(file_path, 'r', errors='ignore') as file: file_content = file.read() # 按时间戳拆分内容以查找单独的部分 # 时间戳的格式为 YYYY-MM-DD HH:MM:SS,因此我们将使用正则表达式根据此模式进行拆分 import re sections = re.split(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\n', file_content) # print(sections) # 如果txt第一个元素为空值(由于拆分),则将其删除 if not sections[0]: sections.pop(0) # 将每个部分放入列表 list = [] # 前面txt有速度直径介绍标识,从第二组数据开始读 for section in sections: # 将字符串拆分为几行,然后按空格拆分每行并转换为 DataFrame lines = section.strip().split('\n') matrix = [line.split() for line in lines] df = pd.DataFrame(matrix) # 计算数浓度公式 # 按行读,一行为一个列表 n = df.iloc[:,0:21].values.astype(float) # 按列读,一列为一个列表 d = df1.iloc[:,0:21].values.astype(float) v = df2.iloc[:].values.astype(float) A = float(0.0054) t = float(60) ND = n / A * t * v * d df3 = pd.DataFrame(ND) sum_b = df3.iloc[:,0:21].sum() print(sum_b) # ND_SUM = ND.sum() # print(ND) # r = n/d # print(r) # 从第一组数据开始,先按列读,从第1列开始读取每行n值 # for col,n in df.items(): # # 从第1列开始读,读取其32行n值,共读取21列1 # n = [float(x) for x in n.values] # n = np.array(n) # # print(cnt) # d = df1.iloc[0:30,:].values.astype(float) # print(d) # v = df2.iloc[:].values.astype(float) # print(v) # 显示每个dataframe形状以确认 # df_shapes = df.shape[n] # print(df_shapes)
标签:pd,python,0.125,float,雨滴,df,file,print,浓度 From: https://www.cnblogs.com/shirleysu90/p/18174312