Python 将函数应用于dataframe的每一行以创建两个新列
我有一个熊猫数据框,Python 将函数应用于dataframe的每一行以创建两个新列,python,pandas,Python,Pandas,我有一个熊猫数据框,st包含多个列: <class 'pandas.core.frame.DataFrame'> DatetimeIndex: 53732 entries, 1993-01-07 12:23:58 to 2012-12-02 20:06:23 Data columns: Date(dd-mm-yy)_Time(hh-mm-ss) 53732 non-null values Julian_Day 53732
st
包含多个列:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 53732 entries, 1993-01-07 12:23:58 to 2012-12-02 20:06:23
Data columns:
Date(dd-mm-yy)_Time(hh-mm-ss) 53732 non-null values
Julian_Day 53732 non-null values
AOT_1020 53716 non-null values
AOT_870 53732 non-null values
AOT_675 53188 non-null values
AOT_500 51687 non-null values
AOT_440 53727 non-null values
AOT_380 51864 non-null values
AOT_340 52852 non-null values
Water(cm) 51687 non-null values
%TripletVar_1020 53710 non-null values
%TripletVar_870 53726 non-null values
%TripletVar_675 53182 non-null values
%TripletVar_500 51683 non-null values
%TripletVar_440 53721 non-null values
%TripletVar_380 51860 non-null values
%TripletVar_340 52846 non-null values
440-870Angstrom 53732 non-null values
380-500Angstrom 52253 non-null values
440-675Angstrom 53732 non-null values
500-870Angstrom 53732 non-null values
340-440Angstrom 53277 non-null values
Last_Processing_Date(dd/mm/yyyy) 53732 non-null values
Solar_Zenith_Angle 53732 non-null values
dtypes: datetime64[ns](1), float64(22), object(1)
尝试将此应用于数据帧会出现错误:
st.apply(calculate, axis=1)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-248-acb7a44054a7> in <module>()
----> 1 st.apply(calculate, axis=1)
C:\Python27\lib\site-packages\pandas\core\frame.pyc in apply(self, func, axis, broadcast, raw, args, **kwds)
4191 return self._apply_raw(f, axis)
4192 else:
-> 4193 return self._apply_standard(f, axis)
4194 else:
4195 return self._apply_broadcast(f, axis)
C:\Python27\lib\site-packages\pandas\core\frame.pyc in _apply_standard(self, func, axis, ignore_failures)
4274 index = None
4275
-> 4276 result = self._constructor(data=results, index=index)
4277 result.rename(columns=dict(zip(range(len(res_index)), res_index)),
4278 inplace=True)
C:\Python27\lib\site-packages\pandas\core\frame.pyc in __init__(self, data, index, columns, dtype, copy)
390 mgr = self._init_mgr(data, index, columns, dtype=dtype, copy=copy)
391 elif isinstance(data, dict):
--> 392 mgr = self._init_dict(data, index, columns, dtype=dtype)
393 elif isinstance(data, ma.MaskedArray):
394 mask = ma.getmaskarray(data)
C:\Python27\lib\site-packages\pandas\core\frame.pyc in _init_dict(self, data, index, columns, dtype)
521
522 return _arrays_to_mgr(arrays, data_names, index, columns,
--> 523 dtype=dtype)
524
525 def _init_ndarray(self, values, index, columns, dtype=None,
C:\Python27\lib\site-packages\pandas\core\frame.pyc in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
5411
5412 # consolidate for now
-> 5413 mgr = BlockManager(blocks, axes)
5414 return mgr.consolidate()
5415
C:\Python27\lib\site-packages\pandas\core\internals.pyc in __init__(self, blocks, axes, do_integrity_check)
802
803 if do_integrity_check:
--> 804 self._verify_integrity()
805
806 self._consolidate_check()
C:\Python27\lib\site-packages\pandas\core\internals.pyc in _verify_integrity(self)
892 "items")
893 if block.values.shape[1:] != mgr_shape[1:]:
--> 894 raise AssertionError('Block shape incompatible with manager')
895 tot_items = sum(len(x.items) for x in self.blocks)
896 if len(self.items) != tot_items:
AssertionError: Block shape incompatible with manager
然后循环遍历所有索引并尝试修改我在其中得到的None
值,但我所做的修改似乎不起作用。也就是说,没有生成错误,但数据帧似乎没有被修改
for i in st.index:
# do calc here
st.ix[i]['a'] = a
st.ix[i]['b'] = b
我原以为这两种方法都能奏效,但两种方法都不行。那么,我做错了什么?最好的、最“pythonic”和“pandaonic”的方法是什么?要使第一种方法起作用,请尝试返回一个序列而不是元组(apply抛出异常,因为它不知道如何将行粘合在一起,因为列数与原始帧不匹配) 第二种方法应适用于以下情况:
st.ix[i]['a'] = a
与:
这在这里得到了解决:
适用于你的问题,这应该有效:
def calculate(s):
a = s['path'] + 2*s['row'] # Simple calc for example
b = s['path'] * 0.153
return pd.Series({'col1': a, 'col2': b})
df = df.merge(df.apply(calculate, axis=1), left_index=True, right_index=True)
我总是使用lambdas和内置的
map()
函数通过组合其他行来创建新行:
st['a'] = map(lambda path, row: path + 2 * row, st['path'], st['row'])
它可能比数字列的线性组合稍微复杂一些。另一方面,我觉得最好采用约定,因为它可以用于更复杂的行组合(例如,使用字符串)或使用其他列的函数在列中填充缺少的数据
例如,假设您有一个包含列SEXT和title的表,但缺少一些标题。您可以使用如下函数填充它们:
title_dict = {'male': 'mr.', 'female': 'ms.'}
table['title'] = map(lambda title,
gender: title if title != None else title_dict[gender],
table['title'], table['gender'])
另一个解决方案基于:
请注意
assign
始终返回数据的副本,保持原始数据帧不变。第二种方法的解决方案有效-谢谢:-)。然而,我无法获得第一种工作方法。返回一个系列是有效的,我得到了一个“mini-df”返回,但我似乎无法将apply
函数返回的值放入原始数据帧中。使用st['a'],st['b']=st.apply(calculate,axis=1)
不起作用,在zip(*)中包装右侧也不起作用。
。你知道我这里做错了什么吗?你可以用pd.concat([df,new\u df],axis=1)将“mini”df列连接到原始数据帧上。您可能还想考虑基于列的操作,例如,计算和添加列“a”,其中包含“代码> df′[a’′]=df(‘路径’)+2 *df[ [行] ] /代码>,这仅适用于我:返回Pd。系列({ COL1′:a,‘COL2’:b}),是特定于Python版本的语法吗?在Python 3中,您将需要使用<代码>元组(map(…))
而不仅仅是映射(…)
。
st.ix[i, 'a'] = a
def calculate(s):
a = s['path'] + 2*s['row'] # Simple calc for example
b = s['path'] * 0.153
return pd.Series({'col1': a, 'col2': b})
df = df.merge(df.apply(calculate, axis=1), left_index=True, right_index=True)
st['a'] = map(lambda path, row: path + 2 * row, st['path'], st['row'])
title_dict = {'male': 'mr.', 'female': 'ms.'}
table['title'] = map(lambda title,
gender: title if title != None else title_dict[gender],
table['title'], table['gender'])
st.assign(a = st['path'] + 2*st['row'], b = st['path'] * 0.153)