【发布时间】:2013-06-23 22:25:30
【问题描述】:
Pandas/Python 新手,我不得不编写一些笨拙的代码。我将不胜感激您将如何执行此操作并加快速度(我将针对千兆字节的数据执行此操作)。
所以,我正在使用 pandas/python 进行一些 ETL 工作。执行逐行计算,因此我需要它们作为流程中的数字类型(这部分省略)。我需要将一些字段作为数组输出,并去掉单引号、nan 和“.0”。
第一个问题,有没有办法在 R 中对这些 if else 语句 ala ifelse 进行矢量化?其次,肯定有更好的方法来删除“.0”。 pandas/numpy 处理数字类型中的空值似乎存在重大问题。
最后,.replace 似乎不适用于单引号的 DataFrame。我错过了什么吗?这是示例代码,如果您对此有任何疑问,请告诉我:
import pandas as pd
# have some nulls and need it in integers
d = {'one' : [1.0, 2.0, 3.0, 4.0],'two' : [4.0, 3.0, NaN, 1.0]}
dat = pd.DataFrame(d)
# make functions to get rid of the ".0" and necessarily converting to strings
def removeforval(val):
if str(val)[-2:] == ".0":
val = str(val)[:len(str(val))-2]
else:
val = str(val)
return val
def removeforcol(col):
col = col.apply(removeforval)
return col
dat = dat.apply(removeforcol,axis=0)
# remove the nan's
dat = dat.replace('nan','')
# need some fields in arrays on a postgres database
quoted = ['{' + str(tuple(x))[1:-1] + '}' for x in dat.to_records(index=False)]
print "Before single quote removal"
print quoted
# try to replace single quotes using DataFrame's replace
quoted_df = pd.DataFrame(quoted).replace('\'','')
quoted_df = quoted_df.replace('\'','')
print "DataFrame does not seem to work"
print quoted_df
# use a loop
for item in range(len(quoted)):
quoted[item] = quoted[item].replace('\'','')
print "This Works"
print quoted
谢谢!
【问题讨论】:
-
你能展示你想要的输出吗?
-
[{4,1},{2,3},{3,},{4,1}] 就像最后一个输出一样
-
我误会它是这样的列表 ['{1, 4}', '{2, 3}', '{3, }', '{4, 1}']
-
好吧,
{也很奇怪,那是字典(而不是元组引用)
标签: python-2.7 replace pandas nan