简体   繁体   English

大熊猫平均值数据框更改初始数据框

[英]pandas mean value data frame changes initial data frame

I have been stumbling upon a problem and have no idea why it behaves like that. 我一直在绊脚石,不知道为什么会这样。 I have pandas DataFrame with hourly values and wanted to make a new DataFrame out of this with the monthly mean values for each hour. 我有带有每小时值的pandas DataFrame,并希望以此为基础制作一个新的DataFrame,其中包含每小时的每月平均值。 This is one part of the code I used: 这是我使用的代码的一部分:

flussmonthly=fluss2 
flussmonthly['2015-06-01 00:00:00' : '2015-06-30 23:00:00']=fluss2['2015-06-01 00:00:00' : '2015-06-30 23:00:00'].mean()

where fluss2 is the initial DataFrame with the hourly values and flussmonthly should in the end be the DataFrame with the monthly mean values. 其中fluss2是带有小时值的初始DataFrame,最后flussmonthly应该是带有月平均值的DataFrame。 however, whenever I apply this code the DataFrame flussmonthly has the average value of this month for each hour, but also the DataFrame fluss2 has. 但是,每当我应用此代码时,DataFrame flussmonthly便具有该月份每个小时的平均值,但DataFrame fluss2也具有。 I have no idea why. 我不知道为什么。 How can it be that also the DataFrame fluss2 changes? DataFrame fluss2也会如何变化?

When you do: 当您这样做时:

flussmonthly=fluss2

You are not copying the data frame, but just copying the reference . 您不是在复制数据框,而是在复制引用 If you want flussmonthly to be a different, independent data frame containing the same data as fluss2 the you should do: 如果希望flussmonthly是一个不同的,独立的数据框,其中包含与fluss2相同的数据,则应执行以下操作:

flussmonthly = fluss2.copy()

Then changing flussmonthly will not affect fluss2 . 然后flussmonthly更改flussmonthly不会影响fluss2

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

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