Python-从csv中选择不同的行值,并将其合并到新的csv中 [英] Python - Select different row values from csv and combine them in new csv

查看:217
本文介绍了Python-从csv中选择不同的行值,并将其合并到新的csv中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个



下面的代码选择1行测量的正确行:

  df = pd.read_csv(csv,标头=无,名称= [调查,时间,潮汐, mwp, swh, mwd, data1, data2, data3, data4, data5 ])
xp = [datetime.strptime(d,%d /%m /%YT%H:%M)for d in df ['time']]

xs = mdates.date2num(xp)
日期= mdates.DateFormatter(%d /%m /%Y\n%H:%M)

#选择行数据波
survey01 = 26/03 / 2019T14:00
survey02 = 10/04 / 2019T14:00
survey03 = 11/04 / 2019T15:00
survey04 = 01/05 / 2019T09:00

#选择行数据波
selected_survey = df.loc [df [ time]。eq(survey01)]。index [0]
wave = df.loc [selected_survey-6:selected_wave, time]。index [0]
result_wave = df.loc [wave,['survey','time','tides','mwp','swh','mwd']]
meas = df.loc [selected_survey:selected_meas,时间 ] .index [0]
result_meas = df.loc [meas,['data1','data2','data3','data4','data5']]

#Join他们在一起
join_list = []
join_list.extend(result_wave)
join_list.extend(result_meas)
print(joined_list)

#导出到csv
data = pd.DataFrame(list(zip(*([joined_list])))。add_prefix('Survey1')
data.to_csv('Waves.csv',index = False)
print(data)

应该对所有测量(总计20个以上)进行此操作,并将其合并1个csv



我如何为所有这些对象执行此操作并将其导出到一个csv文件中?

 调查1 26/03 / 2019T08:00 1.2 9.34 0.509 1.080 25.5 18.4 31.64 27.3 24.2 
调查2 10/04 / 2019T08:00 1.1 8.06 1.232 1.155 24.64 19.46 31.844 28.83 25.357
调查3 ...

一种在csv文件中获取正确数据的简便方法?

解决方案

我无法完全理解代码。但是,如评论中所述,您可以使用 apply()获得所需的结果。

  def process_data(i):
selected_survey = df.loc [df [ time]。eq(i)]。index [0]
wave = df.loc [selected_survey-3:selected_wave, time]。index [0]
result_wave = df.loc [wave,['survey','time','tides','mwp','swh',' mwd']]
meas = df.loc [selected_survey:selected_meas, time]。index [0]
result_meas = df.loc [meas,['data1','data2','data3 ','data4','data5']]]

join_list = []
join_list.extend(result_wave)
join_list.extend(result_meas)
return join_list

join_list = df [ time]。apply(process_data)

survey_index_list = [f'survey {i}'for i in range(len(joined_list))]
data = pd.DataFrame(list(zip(*(joind_list])),index = survey_index_list)
打印(数据)


I have a csv file containing hourly data of wave conditions and data from measurements taken during certain times. I want to select wave conditions 6 hours before the measurement and the outcomes of the measurements. I want to export that to a new csv file for all the measurements.

The code below selects the right rows for 1 measurement:

df = pd.read_csv(csv, header=None, names=['survey', 'time', 'tides', 'mwp', 'swh', 'mwd', 'data1', 'data2', 'data3', 'data4', 'data5'])
xp = [datetime.strptime(d, "%d/%m/%YT%H:%M") for d in df['time']]

xs = mdates.date2num(xp)
date = mdates.DateFormatter ("%d/%m/%Y\n%H:%M")

#select row data waves
survey01 = "26/03/2019T14:00"
survey02 = "10/04/2019T14:00"
survey03 = "11/04/2019T15:00"
survey04 = "01/05/2019T09:00"

#Select row data waves
selected_survey = df.loc[df["time"].eq(survey01)].index[0]
wave = df.loc[selected_survey-6: selected_wave, "time"].index[0]
result_wave = df.loc[wave, ['survey', 'time', 'tides', 'mwp', 'swh', 'mwd']]
meas = df.loc[selected_survey: selected_meas, "time"].index[0]
result_meas = df.loc[meas, ['data1', 'data2', 'data3', 'data4', 'data5']]

#Join them together
joined_list = []
joined_list.extend (result_wave)
joined_list.extend (result_meas)
print (joined_list)

#Export to csv
data = pd.DataFrame(list(zip(*[joined_list]))).add_prefix('Survey1')
data.to_csv('Waves.csv', index=False)
print(data)

This should be done for all the measurements (20+ in total) and combined in 1 csv

How do I do this for all of them and export it to one csv file?

survey 1  26/03/2019T08:00  1.2 9.34    0.509   1.080  25.5  18.4  31.64    27.3    24.2
survey 2  10/04/2019T08:00  1.1 8.06    1.232   1.155  24.64 19.46 31.844   28.83   25.357
survey 3  ...

Or is there an easier way of getting the right data in a csv file?

解决方案

I wasn't able to comprehend the code completely. However, as discussed in the comments, you can use the apply() to get the required results.

def process_data(i):
    selected_survey = df.loc[df["time"].eq(i)].index[0]
    wave = df.loc[selected_survey-3: selected_wave, "time"].index[0]
    result_wave = df.loc[wave, ['survey', 'time', 'tides', 'mwp', 'swh', 'mwd']]
    meas = df.loc[selected_survey: selected_meas, "time"].index[0]
    result_meas = df.loc[meas, ['data1', 'data2', 'data3', 'data4', 'data5']]

    joined_list = []
    joined_list.extend (result_wave)
    joined_list.extend (result_meas)
    return joined_list

joined_list = df["time"].apply(process_data)

survey_index_list = [f'survey{i}' for i in range(len(joined_list))]
data = pd.DataFrame(list(zip(*[joined_list])), index=survey_index_list)
print(data)

这篇关于Python-从csv中选择不同的行值,并将其合并到新的csv中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆