0
def getOHLC_df(df):
    grouped = df.groupby('symbol')
    df_final = pd.DataFrame()
    global csv_data
    for name, group in grouped:
        group = group.sort_values('timestamp')
        timestamp = group['timestamp'].iloc[0]
        symbol = name
        open = group['ltp'].iloc[0]
        close = group['ltp'].iloc[-1]
        high = group['ltp'].max()
        low = group['ltp'].min()
        data = {
            'timestamp': timestamp,
            'symbol': symbol,
            'open': open,
            'close': close,
            'high': high,
            'low': low,}

        df_final = df_final.append(data, ignore_index=True)
        df_final = df_final.reindex(['timestamp', 'symbol', 'open', 'high', 'low', 'close'], axis=1)

        field_names = ['timestamp', 'symbol', 'open', 'close', 'high', 'low']

        with open('C:/Users/choud/Documents/'+symbol+'.csv','a') as f_object:
            # Pass the file object and a list
            # of column names to DictWriter()
            # You will get a object of DictWriter
            dictwriter_object = csv.DictWriter(f_object, fieldnames=field_names)

            # Pass the dictionary as an argument to the Writerow()
            dictwriter_object.writerow(data)

            # Close the file object
            f_object.close()

    print(df_final)

我想将 pandas 数据框中的单行存储在 csv 文件中。我有特定时间的 200 只股票的数据框,比如说从 9:15 到 9:30 。想要在各自的股票 csv 文件中添加特定的行。我有 200 个不同股票的 csv 文件。

我遇到了 TypeError 的问题:'numpy.float64' object is not callable

4

1 回答 1

2

该错误是因为您在代码openopen = group['ltp'].iloc[0]较早定义,然后在尝试执行with open('C:/Users/choud/Documents/'+symbol+'.csv','a') as f_object:

实际上,您已经重新定义open并且它不能再用于打开文件。

于 2021-09-29T09:59:58.310 回答