每次函数运行时,熊猫都会创建新列

时间:2019-07-15 14:05:00

标签: python pandas csv

这是data.csv:

enter image description here

tickers = ['ACOR', 'ACM', 'ACLS', 'ACND', 'ACMR']
stats = ['mkt_cap', 'price', 'change']

此代码为资产目录中的每个统计信息创建一个csv文件:

date = str(dt.date.today())

for stat in stats:
    df = pd.read_csv('data.csv')
    df.set_index('ticker', inplace=True)
    df = df.loc[tickers, ['{}'.format(stat)]]
    date = str(dt.date.today())
    df.rename(columns = {'{}'.format(stat):date}, inplace=True)
    df.to_csv(assets/{}.csv'.format(stats))

这是price.csv

ticker      2019/07/04
ACOR              7.42
ACM              37.33
...             ...

问题是,每次使用当前日期作为标题运行此函数时,我都需要创建一个新列。 Data.csv每天都会更新,我想将新数据添加到mkt_cap.csv,prices.csv和change.csv中,并以新日期作为标题。更新后的prices.csv类似于:

ticker      2019/07/04   2019/07/05
ACOR              7.42          XXX
ACM              37.33          XXX
...             ...

编辑:

date = str(dt.date.today())

for stat in stats:
    df = pd.read_csv('data.csv')
    df.set_index('ticker', inplace=True)
    df = df.loc[tickers, ['{}'.format(stat)]]
    date = str(dt.date.today())
    df.rename(columns = {'{}'.format(stat):date}, inplace=True)
    df.to_csv(assets/{}.csv'.format(stats))

for col in stats.columns:
    stats["{}-{}".format(dt.date.today(),col)] = stats[col]

dataframes = []
for datapoint in stats.columns[-5:-1]:
    dataframes.append(stats[[datapoint, "ticker"]])

for dff in dataframes:
    dff.to_csv('assets/{}.csv'.format(dff.columns[1]))

2 个答案:

答案 0 :(得分:1)

import pandas as pd
list1 = []
for i in range(0,10):
  list1.append(i)
df = pd.DataFrame()
df["col1"] = list1
df['col2'] = df['col1']+5


import datetime as dt
def new_col(df):
  df[dt.datetime.now()] = df['col1']+ df['col2']
  return df

new_col(df)

在函数运行的日期时间调用函数时,这将创建一个新列。不能完全确定您要尝试执行的操作是否达到新列的算法,但这应该可以解决创建新列的问题。

答案 1 :(得分:0)

for col in acor.columns: #or you could just use your stat list
  acor["{}-{}".format(dt.datetime.now(),col)] = acor[col]

dataframes = [] ##seperates into individual dataframes 
for datapoint in acor.columns[-5:-1]:
  dataframes.append(acor[[datapoint,"timestamp"]])###you probobly want to replace timestamp with "symbol" or "ticker"
  ###finally saves dataframes by date and stat 
for dff in dataframes:
  dff.to_csv("{}.csv".format(dff.columns[1]))