English 中文(简体)
我怎么能够计算一大笔CSV档案中两个名列之间的差额,然后将结果保存到第二个CSV档案中?
原标题:How can I calculate the difference between two named columns in a huge CSV file, then save the results to a second CSV file?

我有一份CSV文件,其中载有近2亿行(数据千兆字节)。 它只有5列。 我想重复数据,进行简单的计算,首先是各栏,然后是各行之间。

抽样数据:

DateTime,Width,Length,Count,Age
01.01.2010 00:00:00,0.55,0.25,1,4
07.02.2010 00:00:01,0.53,0.28,2,3
21.02.2010 00:00:01,0.55,0.25,2,3
20.03.2010 00:00:01,0.55,0.25,1,3
09.05.2010 00:00:02,0.55,0.25,4,7
11.05.2010 00:00:02,0.5,0.3,3,5

我用Pandas语阅读了楚克语中的数据,但我不相信如何进入每行各栏以进行基本的算术。

这里,我目前还没有工作。

import pandas as pd

file_in = r"B:UsersuserDocumentshuge-dataset.csv"
file_out = r"B:UsersuserDocumentsaggregate.csv"

data = pd.read_csv(file_in, chunksize=100000)

for idx, chunk in enumerate(data):
    for row in chunk:
        print("row: ", row)
        diff = row[1] - row[2]
        data_out.append([row[0],diff])
        if row[0] == 0:
            prevrow = row
        else:
            rowdiff = row[1] - prevrow[1]

pd.write_csv(file_out, data_out)

例如,我想使用一栏的名称:

<代码>ratio =row[ Width]/row[Length]

我然后想将每一行与以往各行进行比较,例如:

<代码>width_diff = row[width] - prev_row[ width]/code>

任何要点/标准?

问题回答

让我们说数据框架的名称是df。 为了计算第一种公式,你可以轻松写:

ratio = df[ Width ]/df[ Length ]

如果你想将其储存在你的数据库中,你可以写:

df[ ratio ] = df[ Width ]/df[ Length ]

For the second formula you can use diff() function.

width_diff = df[ Width ].diff()

最后,将数据框架储存为文档,可使用<代码>至_csv(功能。 在安达没有<条码>。

df.to_csv(file_out)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签