对表格数据多行取出现次数最多的元素重建表格

在这里插入代码片
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')  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值