简体   繁体   English

如何在每个循环后保存 output in.csv 而不覆盖 Pandas?

[英]How to save output in .csv after every loop without overwriting in Pandas?

I want to save my output in.csv.我想将我的 output 保存在 csv 中。 When I am running my while loop and saving the output, My output is only saving for the last iteration.当我运行我的 while 循环并保存 output 时,我的 output 仅保存最后一次迭代。 Its not saving my all iteration value.它没有保存我所有的迭代值。

Also, I want to skip the zero value rows while printing my output.另外,我想在打印 output 时跳过零值行。

This is my code:这是我的代码:

import pandas as pd `#pandas library
sample = pd.DataFrame(pd.read_csv ("Sample.csv")) #importing .csv as pandas DataFrame

i = 0
while (i <= 23):
    print('Value for', i) `#i vale`
    sample2 = (sample[sample['Hour'] == i])`#Data for every hour`
    sample3 = (sample2[(sample2['GHI']) == (sample2['GHI'].max(0))]) `#Max value from sample3 DataFrame`
    sample3 = sample3.loc[sample3.ne(0).all(axis=1)]`ignoring all rows having zero values`
    print(sample3)  `print sample3`
    sample3.to_csv('Output.csv')`trying to save for output after every iteration`
    i = i + 1

An other way of doing what you want to do is to get rid of your loop, like this:做你想做的另一种方法是摆脱你的循环,像这样:

sample_with_max_ghi = sample.assign(max_ghi=sample.groupby('Hour')['GHI'].transform('max'))
sample_filtered = sample_with_max_ghi[sample_with_max_ghi['GHI'] == sample_with_max_ghi['max_ghi']]
output_sample = sample_filtered.loc[sample_filtered.ne(0).all(axis=1)].drop('max_ghi', axis=1)
output_sample.to_csv('Output.csv')

Some explanations:一些解释:

1. 1.

sample_with_max_ghi = sample.assign(max_ghi=sample.groupby('Hour')['GHI'].transform('max'))

This line add a new column to your dataframe containing the max of GHI column for your group of Hour此行向您的 dataframe 添加一个新列,其中包含您的Hour组的GHI列的最大值

2. 2.

sample_filtered = sample_with_max_ghi[sample_with_max_ghi['GHI'] == sample_with_max_ghi['max_ghi']]

This line filters only rows where the GHI value is actually the max of its Hour group此行仅过滤GHI值实际上是其Hour组的最大值的行

3. 3.

output_sample = sample_filtered.loc[sample_filtered.ne(0).all(axis=1)].drop('max_ghi', axis=1)

And apply the last filter to get rid of the 0 values rows并应用最后一个过滤器以摆脱 0 值行

while the loop is running adding the value at every loop to rename the csv file will make it to look unique and solve your problem.. eg:当循环运行时,在每个循环中添加值以重命名 csv 文件将使其看起来独一无二并解决您的问题.. 例如:

import pandas as pd `#pandas library
sample = pd.DataFrame(pd.read_csv ("Sample.csv")) #importing .csv as pandas DataFrame

i = 0
while (i <= 23):
    print('Value for', i) `#i vale`
    sample2 = (sample[sample['Hour'] == i])`#Data for every hour`
    sample3 = (sample2[(sample2['GHI']) == (sample2['GHI'].max(0))]) `#Max value from sample3 DataFrame`
    sample3 = sample3.loc[sample3.ne(0).all(axis=1)]`ignoring all rows having zero values`
    print(sample3)  `print sample3`
    sample3.to_csv(str(i)+'Output.csv')`trying to save for output after every iteration`
    i = i + 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM