у меня есть следующая функция, которая берет строку и возвращает фрейм данных ld_coefs_df_long
, я не уверен, правильный ли синтаксис функции:
def apply_calc_coef (row):
device_id = row['deviceId']
sound_filename = row['filename']
sound_fullpath = os.path.join(basepath, device_id, sound_filename)
offset = row['offset']
telemetry_id = row['telemetry_id']
fs,data = wavfile.read(sound_fullpath)
ld_coefs_df = calc_ld_coefs(data, fs, offset=offset, win_len=2000, ndeg=12)
ld_coefs_df_long = pd.melt(ld_coefs_df, id_vars=['time'] , var_name='series_type' )
ld_coefs_df_long['telemetry_id'] = telemetry_id
ld_coefs_df_long.rename(columns={"time": "t_seconds"})
return ld_coefs_df_long // is a dataframe
мне нужно применить эту функцию к каждой строкефрейма данных с именем ds_telemetry_pd
и суммируют результаты (которые представляют собой фрейм данных для каждой строки) вместе, чтобы получить новый фрейм данных, используя следующий синтаксис:
ds_telemetry_pd.apply(lambda x: apply_calc_coef(x))
, очевидно, я не в хорошем направлении, так как либомоей функции или способа, которым я применяю t или оба они неверны.
РЕДАКТИРОВАТЬ: когда я использую ds_telemetry_pd.apply(apply_calc_coef, axis=1)
я получаю следующую ошибку:
`cannot copy sequence with size 3 to array axis with dimension 14400`
ValueError Traceback (most recent call last)
<command-3171623043129929> in <module>()
----> 1 ds_telemetry_pd.apply(apply_calc_coef, axis=1)
/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
4150 if reduce is None:
4151 reduce = True
-> 4152 return self._apply_standard(f, axis, reduce=reduce)
4153 else:
4154 return self._apply_broadcast(f, axis)
/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
4263 index = None
4264
-> 4265 result = self._constructor(data=results, index=index)
4266 result.columns = res_index
4267
/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
264 dtype=dtype, copy=copy)
265 elif isinstance(data, dict):
--> 266 mgr = self._init_dict(data, index, columns, dtype=dtype)
267 elif isinstance(data, ma.MaskedArray):
268 import numpy.ma.mrecords as mrecords
/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in _init_dict(self, data, index, columns, dtype)
400 arrays = [data[k] for k in keys]
401
--> 402 return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
403
404 def _init_ndarray(self, values, index, columns, dtype=None, copy=False):
/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
5401
5402 # don't force copy because getting jammed in an ndarray anyway
-> 5403 arrays = _homogenize(arrays, index, dtype)
5404
5405 # from BlockManager perspective
/databricks/python/lib/python3.5/site-packages/pandas/core/frame.py in _homogenize(data, index, dtype)
5712 v = lib.fast_multiget(v, oindex.values, default=NA)
5713 v = _sanitize_array(v, index, dtype=dtype, copy=False,
-> 5714 raise_cast_failure=False)
5715
5716 homogenized.append(v)
/databricks/python/lib/python3.5/site-packages/pandas/core/series.py in _sanitize_array(data, index, dtype, copy, raise_cast_failure)
2950 raise Exception('Data must be 1-dimensional')
2951 else:
-> 2952 subarr = _asarray_tuplesafe(data, dtype=dtype)
2953
2954 # This is to prevent mixed-type Series getting all casted to
/databricks/python/lib/python3.5/site-packages/pandas/core/common.py in _asarray_tuplesafe(values, dtype)
390 except ValueError:
391 # we have a list-of-list
--> 392 result[:] = [tuple(x) for x in values]
393
394 return result
ValueError: cannot copy sequence with size 3 to array axis with dimension 14400 ```