Pandas使用混合数据类型从不同的数据帧中减去列

2024-05-19 10:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个从不同的.csv导入的数据帧

df10=pd.read_csv(path10, usecols=["Registros validados"])
df25=pd.read_csv(path25, usecols=["Registros validados"])

它们是173k行和一列,包含的数据有数字,但是有一些空的度量值,当从csv读取时,这些度量值被视为空字符串(这方面的数字也是如此)

我需要做的很简单,我只需要在两个列都有一个数字时减去它们,然后创建第三个数据帧

我从这个网页的其他帖子中找到了两个想法。以下两个是有效的(没有给我任何错误),因为我主要看到的是.apply,但是当使用的列来自同一个数据帧时总是这样,它们不在这里

“有效”的选项是

list(map(subs_PM, dfpm10, dfpm25))
# Returns ['']

以及

dfpm10.combine(dfpm25, func=subs_PM)
# Actually returns a data frame, but is always empty with ''. 

使用的减法函数是

def subs_PM_old(pm10, pm25):
   try: # Thinking the strings would fail at this
       pm10=int(pm10)
       pm25=int(pm25)
   except: 
       return ' '
   else:
       return pm10-pm25

我想减法中的差异可能是因为数据帧不是数字。所以我做了下面的步骤把数字转换成数字,把字符串保留为字符串

df10=df10.apply(pd.to_numeric, errors='ignore')
df25=df25.apply(pd.to_numeric, errors='ignore')

并将函数更新为

def subs_PM(pm10, pm25):
    boolpm10=isinstance(pm10, (int, long, float, complex)) and not isinstance(pm10, bool)
    boolpm25=isinstance(pm10, (int, long, float, complex)) and not isinstance(pm25, bool)

    if boolpm10 and boolpm25:
        return pm10-pm25
    else:
        return ''

但一切都没变

似乎发生的是,在这两种情况下,减法的函数只用于第一行,然后假设其他项也是这样

有办法改变这种状况吗


显然,这些不是我的数据帧,但是考虑一下这个

df1 = pd.DataFrame({1: range(10)})
df2 = pd.DataFrame({1: [2, 3, '', '', 2, 1, '', 6, 2, 3]})
df1.combine(df2, func=subs_PM)
df1.combine(df2, func=subs_PM_old)
list(map(subs_PM, df1, df2))
list(map(subs_PM_old, df1, df2))

Tags: csv数据return数字intisinstancepdsubs
2条回答

试试这个:

def subs_PM(pm10, pm25):
    #pm10 and pm25 are series... not a single number
    #print(pm10)
    try:
        pm10=pd.to_numeric(pm10)
        pm25=pd.to_numeric(pm25)
        return pm10-pm25
    except:
        return None

df1 = pd.DataFrame({1: range(10)})
df2 = pd.DataFrame({1: [2, 3, '', '', 2, 1, '', 6, 2, 3]})
df1.combine(df2, func=subs_PM)

为了检查所有变体,我定义了如下源数据帧:

df1 = pd.DataFrame({1: [0, '',  2,  3, 4, 5, '', 7, 8, 9]})
df2 = pd.DataFrame({1: [2,  3, '', '', 2, 1,  5, 6, 2, 3]})

目标是要有“对”的参数,其中要么df1要么 df2可以包含一个字符串(从最终结果中排除)

初始操作包括:

  • 连接两个数据帧
  • 将空字符串替换为NaN并删除它们
  • 将类型改回int
  • 为两列指定不同的名称

执行此操作的代码是:

res = df1.join(df2, rsuffix='_2').replace('', np.nan).dropna().astype(int)
res.columns=['c1', 'c2']

对于我的源数据,结果是:

   c1  c2
0   0   2
4   4   2
5   5   1
7   7   6
8   8   2
9   9   3

然后计算差值,将其保存在另一列中:

res['dif'] = res.c1 - res.c2

最终结果是:

   c1  c2  dif
0   0   2   -2
4   4   2    2
5   5   1    4
7   7   6    1
8   8   2    6
9   9   3    6

如果需要,请删除c1c2

相关问题 更多 >

    热门问题