小编Tom*_*Eus的帖子

在 python 中使用 Pandas 时 df.query 产生空结果

我正在学习如何在 python 中使用 Pandas 操作数据。我得到了以下脚本:

import pandas as pd

df = pd.read_table( "t.txt" )    #read in the file
df.columns = [x.strip() for x in df.columns]   #strip spaces in headers
df = df.query('TLD == ".biz"')     #select the rows where TLD == ".biz"
df.to_csv('t.txt', sep='\t')  #write the output to a tab-separated file
Run Code Online (Sandbox Code Playgroud)

但输出文件没有记录,只有标题。当我检查使用

print.df
Run Code Online (Sandbox Code Playgroud)

在选择之前,输出是:

             TLD  Length                                              Words  \
0       .biz           5                                                ...   
1       .biz           4                                                ...   
2       .biz           5                                                ...   
3       .biz           5                                                ...   
4       .biz           3                                                ...   
5       .biz           3 …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

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

如何使用Python和pandas去除标题中的空格

我正在尝试使用python将tab标记的文件加载到pandas中,以便我可以执行不同的查询.不幸的是,文件格式是非常好的(它可以很好地加载到Excel,但我需要以编程方式使用它.

当我做:

>>> print(df.columns)

Index([u'Domain Name                                                      ',
       u'Current Bid ', u'Join By Date (ET)', u'Join By Date (PT)',
       u'Bidders ', u'Seller         ', u'TLD     ', u'Length',
       u'Words                                                                           ',
       u'Word Count',
       u'Categories                                                                                                      ',
       u'Hyphens ', u'Numbers ', u'Auction Type'],
      dtype='object')
Run Code Online (Sandbox Code Playgroud)

我怎样才能修复这么简单的文件df.query('TLD) == "value"')呢?

我每5天收到一个新文件,所以我需要以编程方式进行.

注意 - 请耐心等待,我是脚本和Python的新手

python tabs strip pandas

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

标签 统计

pandas ×2

python ×2

dataframe ×1

strip ×1

tabs ×1