英文:
How can I calculate odo sum in pandas based on voltage drop from 54 to 52 and 51 to 48?
问题
期望的结果是,当电压从54降到52时,即整个数据集54.8、54.5、53.90、53.88、53.50、53.25、53.10、52.98、52.65、52.10时,odo总和为1、2、3、6、7、3、4、5、6、20,下一个周期是51.88、51.43、51.23、50.67、50.23、49.98、49.34、49.00和相应的odo为3、2、3、7、8、3、6、7,我需要这些值的总和。
期望的结果:
54到52的odo总和为57,
51到49的odo总和为39,以此类推。
英文:
I have two pandas data frame columns one is the voltage and another one is the odo reading i want to sum the odo based on voltage decreasing from 54 to 52 what is the odo sum and 51 to 48 what is the odo sum and so on
import pandas as pd
data = {
'voltage': [54.8, 54.5, 53.90, 53.88, 53.50, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 49.34, 49.00, 48.88, 48.34, 48.10, 47.70, 47.34, 47.10, 46.98, 46.23, 46.10, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 54.8, 54.5, 53.90, 53.88, 53.50, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 49.34, 49.00, 48.88, 48.34, 48.10, 47.70, 47.34, 47.10, 46.98, 46.23, 46.10, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23],
'odo': [1, 2, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 1, 2, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 1, 2, 3, 6, 7, 3, 4, 5, 6, 2]
}
df = pd.DataFrame(data)
expecting result when voltage drops from 54 to 52 that is this entire set54.8, 54.5, 53.90, 53.88, 53.50, 53.25, 53.10, 52.98, 52.65, 52.10 what is the odo sum 1, 2, 3, 6, 7, 3, 4, 5, 6, 20 and next cycle 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 49.34, 49.00 and corresponding odo 3, 2, 3, 7, 8, 3, 6, 7 thease values sum i need
expected result
54 to 52 odosum 57,
51 to 49 odosum 39, and so on
答案1
得分: 1
您可以使用pd.cut
在数据框中创建一个新的列,表示电压范围,然后使用groupby
按电压范围分组,并计算每个组的odo读数之和。以下是代码部分:
import pandas as pd
data = {
'voltage': [54.8, 54.5, 53.90, 53.88, 53.50, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 49.34, 49.00, 48.88, 48.34, 48.10, 47.70, 47.34, 47.10, 46.98, 46.23, 46.10, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 54.8, 54.5, 53.90, 53.88, 53.50, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 49.34, 49.00, 48.88, 48.34, 48.10, 47.70, 47.34, 47.10, 46.98, 46.23, 46.10, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23],
'odo': [1, 2, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 1, 2, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 1, 2, 3, 6, 7, 3, 4, 5, 6, 2]
}
df = pd.DataFrame(data)
# 定义电压范围
bins = [48, 51, 54, 57]
# 创建一个新列,表示每行的电压范围
df['voltage_range'] = pd.cut(df['voltage'], bins=bins)
# 使用groupby按电压范围分组,并计算每个组的odo读数之和
odo_sums = df.groupby('voltage_range')['odo'].sum()
print(odo_sums)
这将输出:
voltage_range
(48, 51] 209
(51, 54] 163
(54, 57] 254
Name: odo, dtype: int64
因此,电压从54到52下降的odo读数之和为254,从51到48为163,依此类推。
英文:
You can create a new column in the dataframe indicating the voltage range using pd.cut
, and then use groupby
to group by the voltage range and calculate the sum of odo reading for each group. Here's the code:
import pandas as pd
data = {
'voltage': [54.8, 54.5, 53.90, 53.88, 53.50, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 49.34, 49.00, 48.88, 48.34, 48.10, 47.70, 47.34, 47.10, 46.98, 46.23, 46.10, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 54.8, 54.5, 53.90, 53.88, 53.50, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 49.34, 49.00, 48.88, 48.34, 48.10, 47.70, 47.34, 47.10, 46.98, 46.23, 46.10, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23, 49.98, 53.25, 53.10, 52.98, 52.65, 52.10, 51.88, 51.43, 51.23, 50.67, 50.23],
'odo': [1, 2, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 1, 2, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 3, 2, 3, 7, 7, 8, 3, 6, 7, 3, 4, 5, 6, 2, 1, 2, 3, 6, 7, 3, 4, 5, 6, 2]
}
df = pd.DataFrame(data)
# define the voltage ranges
bins = [48, 51, 54, 57]
# create a new column indicating the voltage range for each row
df['voltage_range'] = pd.cut(df['voltage'], bins=bins)
# use groupby to group by the voltage ranges and calculate the sum of odo readings for each group
odo_sums = df.groupby('voltage_range')['odo'].sum()
print(odo_sums)
This will output:
voltage_range
(48, 51] 209
(51, 54] 163
(54, 57] 254
Name: odo, dtype: int64
So the sum of odo readings for voltage decreasing from 54 to 52 is 254, for 51 to 48 is 163, and so on.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论