gpt4 book ai didi

python - 如何迭代不同数据帧中的行并将其用作其他数据帧中的值?

转载 作者:行者123 更新时间:2023-12-01 07:02:05 25 4
gpt4 key购买 nike

我正在尝试通过其唯一值迭代“df_sum”数据框的“契约(Contract)”列。然后为每个“合约”值创建数据框以进行进一步计算。

以下是单个唯一值的情况。我需要迭代数千个唯一值。

这是 df_sum 的整体外观。 /image/KqNOo.jpg

import pandas as pd
from pandas import read_csv


df_sum = read_csv(r'path_to_sum.csv', sep=",", low_memory=False, index_col=False) #summary file

df_EH = df[df['contract']=='EH60906'] # choose specific contract
df_EH = df_EH.sort_values(by=['datetime']) # sort specific contract data by datetime and assign as df_EH

... data analysis should follow such as summing up power_pos for each contract number etc.

我的尝试如下:

import pandas as pd
from pandas import read_csv
import numpy as np
from datetime import datetime


df_sum = read_csv(r'path_to_sum.csv', sep=",", low_memory=False, index_col=False) #summary file

graph_df = pd.DataFrame()

for contract in df_sum['contract'].unique():
print(contract)
contract_df = df[df['contract'] == contract]
contract_df.set_index('datetime', inplace=True)
contract_df.sort_index(inplace = True)
contract_df."data analysis"

Expected result:
ABABAB.power_pos.sum = 123123
CDCDCD.power_pos.sum = 213124
etc

最佳答案

这个怎么样?

df_sum = pd.read_csv(r'path_to_sum.csv', sep=",", low_memory=False, index_col=False)
grouped = df_sum.groupby("contract")["power_pos"].sum()
print(grouped)

关于python - 如何迭代不同数据帧中的行并将其用作其他数据帧中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58591348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com