我有一个动态的DataFrame,它工作得很好,但当没有数据要添加到DataFrame时,我得到一个错误。因此,我需要一个解决方案来创建一个只有列名的空DataFrame。

现在我有这样的东西:

df = pd.DataFrame(columns=COLUMN_NAMES) # Note that there are now row data inserted.

PS:列名仍然会出现在DataFrame中,这很重要。

但当我像这样使用它时,结果是这样的:

Index([], dtype='object')
Empty DataFrame

“空数据框架”部分很好!但我需要显示的不是索引,而是列。

我发现了一件重要的事情:我正在使用Jinja2将这个DataFrame转换为PDF,因此我调用了一个方法,首先将它输出到HTML,就像这样:

df.to_html()

我想这就是列的缺失之处。

总的来说,我遵循了这个例子:http://pbpython.com/pdf-reports.html。css也是来自链接。这就是我将数据帧发送到PDF的方法:

env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("pdf_report_template.html")
template_vars = {"my_dataframe": df.to_html()}

html_out = template.render(template_vars)
HTML(string=html_out).write_pdf("my_pdf.pdf", stylesheets=["pdf_report_style.css"])

我如何能得到一个数据帧作为NumPy数组或Python列表的索引或列?

我在这里读了一些关于静态方法的文章,我想我理解滥用/过度使用静态方法会导致的问题。但是我并没有真正理解为什么很难模拟静态方法。

我知道其他的模拟框架,比如PowerMock,可以做到这一点,但是为什么Mockito不能呢?

我读了这篇文章,但作者似乎虔诚地反对静态这个词,也许是我的理解能力差。

一个简单的解释/链接就好了。

我想按两列对数据帧进行分组,然后在这些组中对聚合的结果进行排序。

In [167]: df

Out[167]:
   count     job source
0      2   sales      A
1      4   sales      B
2      6   sales      C
3      3   sales      D
4      7   sales      E
5      5  market      A
6      3  market      B
7      2  market      C
8      4  market      D
9      1  market      E


In [168]: df.groupby(['job','source']).agg({'count':sum})

Out[168]:
               count
job    source       
market A           5
       B           3
       C           2
       D           4
       E           1
sales  A           2
       B           4
       C           6
       D           3
       E           7

我现在想在每个组中按降序对“count”列排序,然后只取前三行。得到类似这样的东西:

                count
job     source
market  A           5
        D           4
        B           3
sales   E           7
        C           6
        B           4

I am trying to determine whether there is an entry in a Pandas column that has a particular value. I tried to do this with if x in df['id']. I thought this was working, except when I fed it a value that I knew was not in the column 43 in df['id'] it still returned True. When I subset to a data frame only containing entries matching the missing id df[df['id'] == 43] there are, obviously, no entries in it. How to I determine if a column in a Pandas data frame contains a particular value and why doesn't my current method work? (FYI, I have the same problem when I use the implementation in this answer to a similar question).

我试图读取一个大的csv文件(aprox。6 GB)在熊猫和我得到一个内存错误:

MemoryError                               Traceback (most recent call last)
<ipython-input-58-67a72687871b> in <module>()
----> 1 data=pd.read_csv('aphro.csv',sep=';')

...

MemoryError: 

有什么帮助吗?

我有3个CSV文件。每个数据框架的第一列都是人的(字符串)名,而每个数据框架中的所有其他列都是人的属性。

我如何将所有三个CSV文档“联接”在一起,以创建一个单一的CSV,其中每行都具有人的字符串名称的每个唯一值的所有属性?

pandas中的join()函数指定我需要一个多索引,但我对分层索引方案与基于单个索引进行连接有什么关系感到困惑。

我在我的项目中使用模版把手。是否有一种方法可以获得句柄中“each”helper的当前迭代的索引?

<tbody>
     {{#each item}}
         <tr>
            <td><!--HOW TO GET ARRAY INDEX HERE?--></td>
            <td>{{this.key}}</td>
            <td>{{this.value}}</td>
         </tr>
     {{/each}}
</tbody>

我有以下数据框架:

> df1
  id  begin conditional confidence discoveryTechnique  
0 278    56       false        0.0                  1   
1 421    18       false        0.0                  1 

> df2
   concept 
0  A  
1  B

如何对下标进行归并得到:

  id  begin conditional confidence discoveryTechnique concept 
0 278    56       false        0.0                  1       A 
1 421    18       false        0.0                  1       B

我问是因为这是我的理解,合并()即df1.merge(df2)使用列来进行匹配。事实上,这样做我得到:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4618, in merge
    copy=copy, indicator=indicator)
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 58, in merge
    copy=copy, indicator=indicator)
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 491, in __init__
    self._validate_specification()
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/merge.py", line 812, in _validate_specification
    raise MergeError('No common columns to perform merge on')
pandas.tools.merge.MergeError: No common columns to perform merge on

在索引上合并是不好的做法吗?不可能吗?如果是这样,我如何将索引移到一个名为“index”的新列中?

我有一个问题,查看以下数据框架:

n = 100
foo = DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)
foo

问题是它不会在ipython notebook中按默认值打印所有行,但我必须切片才能查看结果行。即使下面的选项也不会改变输出:

pd.set_option('display.max_rows', 500)

有人知道如何显示整个数组吗?