【问题标题】:calculate value of a column for a row when data in same row is added to another column当同一行中的数据添加到另一列时,计算该行的列值
【发布时间】:2019-06-30 15:35:38
【问题描述】:

我在 python 中有一个大熊猫数据框。我有七列原始数据会定期更新一次,每次将新数据添加到第 1-7 列的底部时,我都需要更新其他 84 列的新行中的值。我想这样做而不必重新计算整个 84 列的所有值。因为这些列中有数百万行。

【问题讨论】:

  • 您的问题似乎很有趣,但我无法理解。可以举个例子吗?
  • 例如,我从以下数据框开始:
  • TP1df = pd.DataFrame({'c1':[],'c2':[],'c3':[],'c4':[],'c5':[], 'c6':[],'c7':[]})
  • 然后我运行:TP1df = pd.read_csv(f'C:/filelocation_{x}#{y}.csv', delimiter=',',names = ['c1','c2 ','c3','c4','c5','c6','c7'])
  • 加载一些原始数据。然后我运行大约 84 次计算并根据这些计算在数据框中创建 84 个新列

标签: python pandas moving-average


【解决方案1】:

在对主数据框进行第一次计算后,尝试分别对新数据进行计算,然后在最后将它们连接起来(前提是在连接前两者具有相同的列)。

import pandas as pd

columns = ['c1','c2','c3','c4','c5','c6','c7']

main = pd.read_csv('file.csv', names=columns)
# ... do your calculation

new = pd.read_csv('new_file.csv', names=columns)
# ... do your calculation

all = pd.concat([main, new])

# if you need to reset the index, use the following line instead:
# all = pd.concat([main, new], ignore_index=True)

【讨论】:

  • 我正在计算移动平均线,所以这还不够,第 8-91 列的新行中的计算必须能够处理第 1 列之前行中的所有数据-7
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 1970-01-01
相关资源
最近更新 更多