我有像下面的SampleDf數據的數據。我試圖檢查數據框中一列中的值,看它們是否包含'sum'或'count'或'Avg',然後創建一個新值爲'sum','count'或'Avg'的列。當我在我的真實數據框上運行下面的代碼時,我得到下面的錯誤。當我在我的真實數據框上運行dtypes時,它說所有的列都是對象。下面的代碼與下面的帖子有關。不幸的是,我在我提供的SampleDf上運行代碼時沒有得到相同的錯誤,但是我無法發佈我的整個數據框。熊貓錯誤匹配字符串
後: Pandas and apply function to match a string
Code:
SampleDf=pd.DataFrame([['tom',"Avg(case when Value1 in ('Value2') and [DateType] in ('Value3') then LOS end)"],['bob',"isnull(Avg(case when XferToValue2 in (1) and DateType in ('Value3') and [Value1] in ('HM') then LOS end),0)"]],columns=['ReportField','OtherField'])
search1='Sum'
search2='Count'
search3='Avg'
def Agg_type(x):
if search1 in x:
return 'sum'
elif search2 in x:
return 'count'
elif search3 in x:
return 'Avg'
else:
return 'Other'
SampleDf['AggType'] = SampleDf['OtherField'].apply(Agg_type)
SampleDf.head()
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-17-a2b4920246a7> in <module>()
17 return 'Other'
18
---> 19 SampleDf['AggType'] = SampleDf['OtherField'].apply(Agg_type)
20
21 #SampleDf.head()
C:\Users\Name\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
2292 else:
2293 values = self.asobject
-> 2294 mapped = lib.map_infer(values, f, convert=convert_dtype)
2295
2296 if len(mapped) and isinstance(mapped[0], Series):
pandas\src\inference.pyx in pandas.lib.map_infer (pandas\lib.c:66124)()
<ipython-input-17-a2b4920246a7> in Agg_type(x)
8
9 def Agg_type(x):
---> 10 if search1 in x:
11 return 'sum'
12 elif search2 in x:
TypeError: argument of type 'float' is not iterable
我不能用這些數據重現你的錯誤,儘管如果我用浮點數列表在'['OtherField']'列上寫字,你的函數看起來很好 - 問題似乎與你的''' ['OtherField']'列。 – cmaher
是否okey,如果我提供一個解決方案,工程b與你的不同嗎?只是因爲你的代碼對我也沒有提出任何錯誤 –
@RayhaneMama謝謝你回覆我,是的請提供你的解決方案。 – ndderwerdo