小编inv*_*ker的帖子

使用read_csv将财务数据导入Python Pandas

我有一个.csv具有以下结构:

date_begin,date_end,name,name_code,active_accounts,transaction_amount,transaction_count
1/1/2008,1/31/2008,Name_1,1001,"123,456","$7,890,123.45","67,890"
2/1/2008,2/29/2008,Name_1,1001,"43,210","$987,654.32","109,876"
3/1/2008,3/31/2008,Name_1,1001,"485,079","$1,265,789,433.98","777,888"
...
12/1/2008,12/31/2008,Name_1,1001,"87,543","$432,098,987","87,987"
1/1/2008,1/31/2008,Name_2,1002,"268,456","$890,123.45","97,890"
2/1/2008,2/29/2008,Name_2,1002,"53,210","$987,654.32","109,876"
...
etc
Run Code Online (Sandbox Code Playgroud)

我试图通过使用以下代码将它们读入pandas数据帧:

import pandas as pd

data = pd.read_csv('my_awesome_csv.csv'),parse_dates=[[0,1]],
                   infer_datetime_format=True)
Run Code Online (Sandbox Code Playgroud)

这很好用,除了我想控制每列中的数据类型.当我在解释器中运行以下代码时,我发现引号中的数字不会被识别为数字,无论是美元还是其他数字.

In [10]: data.dtypes
Out[10]: 
date_begin_date_end       object
name                      object
name_code                  int64
active_accounts           object  # Problem, I want this to be a number
transaction_amount        object  # Ditto, I want this to be a number (it's a dollar amount)
transaction_count         object  # Still a number!
dtype: object
Run Code Online (Sandbox Code Playgroud)

我在Pandas csv文档中做了一些窥探,但是在csv中用逗号和美元符号保存为字符串时,我没有找到关于声明类型的数据.我的最终目标是能够对这些列中的值进行一些算术运算.

有什么想法吗?

python csv import pandas

10
推荐指数
1
解决办法
1万
查看次数

在 Pandas 中按组计算同比增长

我有以下几点dataframe

In [1]: df
Out[1]: 

ID            Month           Transaction_Amount

1             2013/01         10
1             2013/02         20
1             2013/03         10
1             2013/04         20
1             2013/05         10
1             2013/06         20
1             2013/07         10
1             2013/08         20
1             2013/09         10
1             2013/10         20
1             2013/11         10
1             2013/12         20
1             2014/01         15
1             2014/02         25
1             2014/03         15
1             2014/04         25
...
1             2014/11         15
1             2014/12         25
...
10000000      2014/11         13
10000000      2014/12         23
Run Code Online (Sandbox Code Playgroud)

我想这样做是计算增长滚动月期间较去年同期,因此,例如,我想找到的值(2014/01 - 2013/01) / (2014/01)(15 …

python indexing function pandas

9
推荐指数
2
解决办法
2万
查看次数

从 Pandas DataFrame 在 Netezza 上创建临时表

我希望将我的工作流程从 SAS 更改为 Python,到目前为止,除了一件非常重要的事情之外,我已经取得了相当大的成功。我不知道如何将 Pandas DataFrames 上传到我公司的 Netezza 以用于以后的查询。这实际上非常重要,因为我们有许多数据集可以上传并用于查询。

我有以下熊猫数据帧:

[In ]  df
[Out]
       col1  col2  col3  
    0     1     2     3
    1     4     5     6
    2     7     8     9
Run Code Online (Sandbox Code Playgroud)

我想通过 ODBC 连接将此 DataFrame 上传到我的 Netezza 盒子。连接已经建立如下:

import pyodbc
conn = pyodbc.connect("Driver=NetezzaSQL;Server=...;")
Run Code Online (Sandbox Code Playgroud)

我已将此连接与 Pandas 结合使用read_sql来提取数据并将其存储在 DataFrame 中。但是,我还没有弄清楚如何提取数据。在 SAS 中,我会执行以下操作:

proc sql _method;
connect to netezza as net_dw
(auth domain info goes here...)
execute( create temporary table my_table
                                ( col1   int,
                                  col2   int,
                                  col3   int ) distribute on (col1) by …
Run Code Online (Sandbox Code Playgroud)

python sql odbc netezza pandas

5
推荐指数
1
解决办法
1791
查看次数

使用Python将.csv文件分成多个块

我有一个很大的.csv文件,大小超过300 GB。我想将其分块成每个100,000,000行的较小文件(每行大约有55-60字节)。

我写了以下代码:

import pandas as pd
df = pd.read_csv('/path/to/really/big.csv',header=None,chunksize=100000000)
count = 1
for chunk in df:
    name = '/output/to/this/directory/file_%s.csv' %s count
    chunk.to_csv(name,header=None,index=None)
    print(count)
    count+=1
Run Code Online (Sandbox Code Playgroud)

这段代码可以正常工作,并且我在磁盘上有足够的内存来一次存储大约5.5-6 GB的内存,但是速度很慢

有没有更好的办法?

编辑

我编写了以下迭代解决方案:

with open('/path/to/really/big.csv', 'r') as csvfile:
    read_rows = csv.reader(csvfile)
    file_count = 1
    row_count = 1
    f = open('/output/to/this/directory/file_%s.csv' %s count,'w')
    for row in read_rows:
        f.write(''.join(row))
        row_count+=1
        if row_count % 100000000 == 0:
            f.close()
            file_count += 1
            f = open('/output/to/this/directory/file_%s.csv' %s count,'w')
Run Code Online (Sandbox Code Playgroud)

编辑2

我想提请注意Vor关于使用Unix / Linux split命令的评论,这是我找到的最快的解决方案。

python csv pandas

2
推荐指数
2
解决办法
4658
查看次数

标签 统计

pandas ×4

python ×4

csv ×2

function ×1

import ×1

indexing ×1

netezza ×1

odbc ×1

sql ×1