我整个上午一直在这里,并慢慢拼凑起来.但是对于我的生活,我无法弄清楚如何在Pandas中使用.str.startswith()函数.
我的XLSX电子表格如下
1 Name, Registration Date, Phone number
2 John Doe, 2015-11-20T19:54:45Z, 1.1112223333
3 Jane Doe, 2015-11-20T20:44:26Z, 65.1112223333
etc...
Run Code Online (Sandbox Code Playgroud)
所以我将它作为数据框导入,清理标题以便没有空格等,然后我想删除任何不以'1'开头的行.(或保留以"1"开头的行)并删除所有其他行.因此,在这个简短的例子中,删除整个'Jane Doe'条目,因为她的电话号码以'65开头'.
import pandas as pd
df = pd.read_excel('testingpanda.xlsx', sheetname = 'Export 1')
def colHeaderCleaner():
cols = df.columns
cols = cols.map(lambda x: x.replace(' ', '_') if isinstance(x, (str, unicode)) else x)
df.columns = cols
df.columns = [x.lower() for x in df.columns]
colHeaderCleaner()
#by default it sets the values in 'registrant_phone' as float64, so this is fixing that...
df['registrant_phone'] = df['registrant_phone'].astype('object')
Run Code Online (Sandbox Code Playgroud)
我得到的最接近的,我的意思是我能够执行的唯一一行没有烦人的追溯和其他错误: …
这有点奇怪,但我正在运行一个名为scrapebox的程序,scrapebox有一个automator插件,可以创建一个文件来自动运行一些内容.为了从cmd运行automator,我将cd进入程序目录,然后键入:
Scrapebox.exe "automator:1.sbaf"
Run Code Online (Sandbox Code Playgroud)
它首先会启动Scrapebox程序,一旦打开,就会立即运行自动化文件.
这是一个更大拼图中的一小部分.我试图在更大的Python脚本中调用它.
import os
import subprocess
..........
..........
..........
print "Opening Scrapebox now, please wait."
os.chdir('C:\Users\Admin\DomainDB\Programs\ScrapeBox')
print
print "Current working dir : %s" % os.getcwd()
print
subprocess.call(["Scrapebox.exe"])
#"automator:1.sbaf"
print "Scrapebox finished. Moving on."
Run Code Online (Sandbox Code Playgroud)
当我像上面那样运行它时,它可以工作并打开scrapebox.但是,我真正需要做的是这样的事情:
subprocess.call(["Scrapebox.exe "automator:1.sbaf""])
Run Code Online (Sandbox Code Playgroud)
当我这样做时,它会抛出语法错误.那么我如何输入可能作为原始字符串,就好像它被输入cmd一样?