在这里插入代码片
import numpy as np
import pandas as pd
import os
weather = pd.read_csv('dataset/train_data/city_B/weather.csv',
sep=',',
header=None,
names=['data','hour','temperature','humidity','wind_direction','wind_speed','wind_force','weather1'])
data_2=[]
temperature_2=[]
humidity_2=[]
wind_direction_2=[]
wind_speed_2=[]
wind_force_2=[]
weather1_2=[]
#将nan值用上一个补上
weather.data=weather.data.fillna(method='pad')
weather.hour=weather.hour.fillna(method='pad')
weather.temperature=weather.temperature.fillna(method='pad')
weather.humidity=weather.humidity.fillna(method='pad')
weather.wind_direction=weather.wind_direction.fillna(method='pad')
weather.wind_speed=weather.wind_speed.fillna(method='pad')
weather.wind_force=weather.wind_force.fillna(method='pad')
weather.weather1=weather.weather1.fillna(method='pad')
#
for i in range(int(len(weather)/24)):
data=weather.data.iloc[i*24:(i+1)*24]
hour=weather.hour.iloc[i*24:(i+1)*24]
temperature=weather.temperature.iloc[i*24:(i+1)*24]
humidity=weather.humidity.iloc[i*24:(i+1)*24]
wind_direction=weather.wind_direction.iloc[i*24:(i+1)*24]
wind_speed=weather.wind_speed.iloc[i*24:(i+1)*24]
wind_force=weather.wind_force.iloc[i*24:(i+1)*24]
weather1=weather.weather1.iloc[i*24:(i+1)*24]
data_1=Counter(data).most_common(1)[0][0]
hour_1=Counter(hour).most_common(1)[0][0]
temperature_1=Counter(temperature).most_common(1)[0][0]
humidity_1=Counter(humidity).most_common(1)[0][0]
wind_direction_1=Counter(wind_direction).most_common(1)[0][0]
wind_speed_1=Counter(wind_speed).most_common(1)[0][0]
wind_force_1=Counter(wind_force).most_common(1)[0][0]
weather1_1=Counter(weather1).most_common(1)[0][0]
data_2.append(data_1)
temperature_2.append(temperature_1)
humidity_2.append(humidity_1)
wind_direction_2.append(wind_direction_1)
wind_speed_2.append(wind_speed_1)
wind_force_2.append(wind_force_1)
weather1_2.append(weather1_1)
from pandas.core.frame import DataFrame
# data=[data_2,temperature_2,humidity_2,wind_direction_2,wind_speed_2,wind_force_2,weather1_2]
# data=DataFrame(data)
data_2=DataFrame(data_2)
temperature_2=DataFrame(temperature_2)
humidity_2=DataFrame(humidity_2)
wind_direction_2=DataFrame(wind_direction_2)
wind_speed_2=DataFrame(wind_speed_2)
wind_force_2=DataFrame(wind_force_2)
weather1_2=DataFrame(weather1_2)
data = pd.concat([data_2,temperature_2,humidity_2,wind_direction_2,wind_speed_2,wind_force_2,weather1_2],axis=1)
data.to_csv(os.path.join('dataset/weathernew.csv'),
header=None,
index=None,
float_format = '%.4f')
对表格数据多行取出现次数最多的元素重建表格
最新推荐文章于 2021-12-07 20:54:49 发布