我有一個 Pandas DataFrame df,有一列df['auc_all']包含一個具有兩個值的元組(例如(0.54, 0.044))
當我運行時:
type(df['auc_all'][0])
>>> str
然而,當我運行時:
def convert_str_into_tuple(self, string):
splitted_tuple = string.split(',')
value1 = float(splitted_tuple[0][1:])
value2 = float(splitted_tuple[1][1:-1])
return (value1, value2)
df['auc_all'] = df['auc_all'].apply(convert_str_into_tuple)
我收到以下錯誤:
df = full_df.create_full()
Traceback (most recent call last):
File "<ipython-input-437-34fc05204bad>", line 18, in create_full
df['auc_all'] = df['auc_all'].apply(self.convert_str_into_tuple)
File "C:\Users\20200016\Anaconda3\lib\site-packages\pandas\core\series.py", line 4357, in apply
return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
File "C:\Users\20200016\Anaconda3\lib\site-packages\pandas\core\apply.py", line 1043, in apply
return self.apply_standard()
File "C:\Users\20200016\Anaconda3\lib\site-packages\pandas\core\apply.py", line 1099, in apply_standard
mapped = lib.map_infer(
File "pandas\_libs\lib.pyx", line 2859, in pandas._libs.lib.map_infer
File "<ipython-input-437-34fc05204bad>", line 63, in convert_str_into_tuple
splitted_tuple = string.split(',')
AttributeError: 'tuple' object has no attribute 'split'
這似乎表明該單元格包含一個元組。
然而:
df['auc'][0][0]
>>> '('
似乎變數型別根據我使用它的位置而變化。這真的發生了嗎?
uj5u.com熱心網友回復:
如果您的列包含字串形式的元組,請使用pd.eval:
df['auc_all'] = pd.eval(df['auc_all'])
例子:
# df = pd.DataFrame({'auc_all': ['(0.54, 0.044)']})
>>> df
auc_all
0 (0.54, 0.044)
>>> type(df['auc_all'][0])
str
# df['auc_all'] = pd.eval(df['auc_all'])
>>> df
auc_all
0 [0.54, 0.044]
>>> type(df['auc_all'][0])
list
缺點是您的元組被轉換為串列,但您可以literal_eval從ast模塊使用:
# import ast
# df['auc_all'] = df['auc_all'].apply(ast.literal_eval)
>>> df
auc_all
0 (0.54, 0.044)
>>> type(df['auc_all'][0])
tuple
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371351.html
