【问题标题】:Add one dataframe after another but increase the index一个接一个地添加数据框,但增加索引
【发布时间】:2020-08-06 09:15:37
【问题描述】:

我有一个original dataframeone other that I would like to add to the first。但是,有一列带有 ID,我希望将数据帧行添加到其中,以从第一个数据帧的最高 QID 增加。我知道如何一个接一个地添加一个数据框。第二个的列名包含在第一个中。

df_qb.append(dfgrouped)

直到今天我都试图在原始数据帧的 QID 列中获取最大值。

# get highest QID and start the QID of the appended rows from here
max_qid = df_qb.QID.astype(dtype = int, errors = 'ignore').max()

但它让我回来了:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-92-03bff1b7ef93> in <module>
      1 # get highest QID and start the QID of the appended rows from here
----> 2 max_qid = df_qb.QID.astype(dtype = int, errors = 'ignore').max()

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in stat_func(self, axis, skipna, level, numeric_only, **kwargs)
  11213             return self._agg_by_level(name, axis=axis, level=level, skipna=skipna)
  11214         return self._reduce(
> 11215             f, name, axis=axis, skipna=skipna, numeric_only=numeric_only
  11216         )
  11217 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in _reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds)
   3889                 )
   3890             with np.errstate(all="ignore"):
-> 3891                 return op(delegate, skipna=skipna, **kwds)
   3892 
   3893         # TODO(EA) dispatch to Index

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\nanops.py in f(values, axis, skipna, **kwds)
    123                     result = alt(values, axis=axis, skipna=skipna, **kwds)
    124             else:
--> 125                 result = alt(values, axis=axis, skipna=skipna, **kwds)
    126 
    127             return result

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\nanops.py in reduction(values, axis, skipna, mask)
    835                 result = np.nan
    836         else:
--> 837             result = getattr(values, meth)(axis)
    838 
    839         result = _wrap_results(result, dtype, fill_value)

C:\ProgramData\Anaconda3\lib\site-packages\numpy\core\_methods.py in _amax(a, axis, out, keepdims, initial, where)
     28 def _amax(a, axis=None, out=None, keepdims=False,
     29           initial=_NoValue, where=True):
---> 30     return umr_maximum(a, axis, None, out, keepdims, initial, where)
     31 
     32 def _amin(a, axis=None, out=None, keepdims=False,

TypeError: '>=' not supported between instances of 'str' and 'float'

【问题讨论】:

    标签: python python-3.x pandas dataframe merge


    【解决方案1】:

    如果检查Series.astype:

    错误{'raise', 'ignore'}, 默认 'raise'
    控制对提供的 dtype 的无效数据引发异常。

    raise : 允许引发异常
    忽略:抑制异常。 出错时返回原始对象

    所以你需要to_numericerrors = 'coerce' 来将值转换为数字:

    max_qid = pd.to_numeric(df_qb.QID, errors = 'coerce').max()
    dfgrouped['QID'] = np.arange(max_qid + 1, max_qid + len(dfgrouped) + 1)
    
    df = df_qb.append(dfgrouped)
    

    【讨论】:

    • 非常感谢!我怎样才能在QID 列中使用这个数字附加一个新的数据框?
    • @RevolucionforMonica - 不确定是否 100% 理解,如果需要用来自 max_qid 的连续值覆盖 dfgrouped['QID'] 列,请使用编辑后的答案。
    猜你喜欢
    • 2019-09-30
    • 1970-01-01
    • 2019-07-28
    • 2021-10-27
    • 2013-10-16
    • 1970-01-01
    • 2017-08-30
    • 2016-11-17
    • 2021-04-17
    相关资源
    最近更新 更多