pandas change int to str

source:

https://stackoverflow.com/questions/17950374/converting-a-column-within-pandas-dataframe-from-int-to-string


In [16]: df = DataFrame(np.arange(10).reshape(5,2),columns=list('AB'))

In [17]: df
Out[17]: 
   A  B
0  0  1
1  2  3
2  4  5
3  6  7
4  8  9

In [18]: df.dtypes
Out[18]: 
A    int64
B    int64
dtype: object

Convert a series

In [19]: df['A'].apply(str)
Out[19]: 
0    0
1    2
2    4
3    6
4    8
Name: A, dtype: object

In [20]: df['A'].apply(str)[0]
Out[20]: '0'

Convert the whole frame

In [21]: df.applymap(str)
Out[21]: 
   A  B
0  0  1
1  2  3
2  4  5
3  6  7
4  8  9

In [22]: df.applymap(str).iloc[0,0]
Out[22]: '0'

TypeError Traceback (most recent call last) Cell In[4], line 7 1 # 定义填充策略 2 fill_values = { 3 '驱动电机速': 0, 4 '驱动电机矩': 0, 5 '电机控制器直流母线电流': 0, 6 '电机控制器输入电压': 0, ----> 7 '驱动电机温度': df['驱动电机温度'].mean(), 8 '驱动电机控制器温度': df['驱动电机控制器温度'].mean() 9 } 11 # 对特定列进行填充 12 for column, value in fill_values.items(): File D:\anaconda\Lib\site-packages\pandas\core\series.py:6549, in Series.mean(self, axis, skipna, numeric_only, **kwargs) 6541 @doc(make_doc("mean", ndim=1)) 6542 def mean( 6543 self, (...) 6547 **kwargs, 6548 ): -> 6549 return NDFrame.mean(self, axis, skipna, numeric_only, **kwargs) File D:\anaconda\Lib\site-packages\pandas\core\generic.py:12420, in NDFrame.mean(self, axis, skipna, numeric_only, **kwargs) 12413 def mean( 12414 self, 12415 axis: Axis | None = 0, (...) 12418 **kwargs, 12419 ) -> Series | float: > 12420 return self._stat_function( 12421 "mean", nanops.nanmean, axis, skipna, numeric_only, **kwargs 12422 ) File D:\anaconda\Lib\site-packages\pandas\core\generic.py:12377, in NDFrame._stat_function(self, name, func, axis, skipna, numeric_only, **kwargs) 12373 nv.validate_func(name, (), kwargs) 12375 validate_bool_kwarg(skipna, "skipna", none_allowed=False) > 12377 return self._reduce( 12378 func, name=name, axis=axis, skipna=skipna, numeric_only=numeric_only 12379 ) File D:\anaconda\Lib\site-packages\pandas\core\series.py:6457, in Series._reduce(self, op, name, axis, skipna, numeric_only, filter_type, **kwds) 6452 # GH#47500 - change to TypeError to match other methods 6453 raise TypeError( 6454 f"Series.{name} does not allow {kwd_name}={numeric_only} " 6455 "with non-numeric dtypes." 6456 ) -> 6457 return op(delegate, skipna=skipna, **kwds) File D:\anaconda\Lib\site-packages\pandas\core\nanops.py:147, in bottleneck_switch.__call__.<locals>.f(values, axis, skipna, **kwds) 145 result = alt(values, axis=axis, skipna=skipna, **kwds) 146 else: --> 147 result = alt(values, axis=axis, skipna=skipna, **kwds) 149 return result File D:\anaconda\Lib\site-packages\pandas\core\nanops.py:404, in _datetimelike_compat.<locals>.new_func(values, axis, skipna, mask, **kwargs) 401 if datetimelike and mask is None: 402 mask = isna(values) --> 404 result = func(values, axis=axis, skipna=skipna, mask=mask, **kwargs) 406 if datetimelike: 407 result = _wrap_results(result, orig_values.dtype, fill_value=iNaT) File D:\anaconda\Lib\site-packages\pandas\core\nanops.py:719, in nanmean(values, axis, skipna, mask) 716 dtype_count = dtype 718 count = _get_counts(values.shape, mask, axis, dtype=dtype_count) --> 719 the_sum = values.sum(axis, dtype=dtype_sum) 720 the_sum = _ensure_numeric(the_sum) 722 if axis is not None and getattr(the_sum, "ndim", False): File D:\anaconda\Lib\site-packages\numpy\core\_methods.py:49, in _sum(a, axis, dtype, out, keepdims, initial, where) 47 def _sum(a, axis=None, dtype=None, out=None, keepdims=False, 48 initial=_NoValue, where=True): ---> 49 return umr_sum(a, axis, dtype, out, keepdims, initial, where) TypeError: can only concatenate str (not "int") to str
最新发布
03-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值