【问题标题】:all pandas columns showing up as string [duplicate]所有熊猫列都显示为字符串[重复]
【发布时间】:2020-04-09 13:35:11
【问题描述】:

我正在从数据库中获取数据,我怀疑其中的所有数据都只是设置为字符串而不是浮点数、整数等。当我将数据导入熊猫数据框时,它都显示为字符串。

print("products.dtypes")
product_category_name         object
product_description_lenght    object
product_height_cm             object
product_id                    object
product_length_cm             object
product_name_lenght           object
product_photos_qty            object
product_weight_g              object
product_width_cm              object
dtype: object

print (products.applymap(type))

结果:

product_category_name product_description_lenght product_height_cm  \
0             <class 'str'>              <class 'str'>     <class 'str'>   
1             <class 'str'>              <class 'str'>     <class 'str'>   
2             <class 'str'>              <class 'str'>     <class 'str'>   
3             <class 'str'>              <class 'str'>     <class 'str'>   
4             <class 'str'>              <class 'str'>     <class 'str'>   
...                     ...                        ...               ...   
32946         <class 'str'>              <class 'str'>     <class 'str'>   
32947         <class 'str'>              <class 'str'>     <class 'str'>   
32948         <class 'str'>              <class 'str'>     <class 'str'>   
32949         <class 'str'>              <class 'str'>     <class 'str'>   
32950         <class 'str'>              <class 'str'>     <class 'str'>   

          product_id product_length_cm product_name_lenght product_photos_qty  \
0      <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
1      <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
2      <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
3      <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
4      <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
...              ...               ...                 ...                ...   
32946  <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
32947  <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
32948  <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
32949  <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   
32950  <class 'str'>     <class 'str'>       <class 'str'>      <class 'str'>   

      product_weight_g product_width_cm  
0        <class 'str'>    <class 'str'>  
1        <class 'str'>    <class 'str'>  
2        <class 'str'>    <class 'str'>  
3        <class 'str'>    <class 'str'>  
4        <class 'str'>    <class 'str'>  
...                ...              ...  
32946    <class 'str'>    <class 'str'>  
32947    <class 'str'>    <class 'str'>  
32948    <class 'str'>    <class 'str'>  
32949    <class 'str'>    <class 'str'>  
32950    <class 'str'>    <class 'str'>  

[32951 rows x 9 columns]

当我查看数据时,有def。那里的数值。我试图取一个值并将其加 1 无济于事。

products['test'] = products['product_description_lenght'] + 1
TypeError: can only concatenate str (not "int") to str

我试过 str.isnumeric 但一切都显示为非数字。

我可以做些什么来检测数值吗?

【问题讨论】:

  • products['product_description_lenght'].astype(float)
  • @JoranBeasley 运行时出现此错误 - ValueError: could not convert string to float:
  • 您只能将存储为对象的数值转换为 int 或 float。否则 pandas 会抛出 'could not convert string' 错误

标签: python pandas


【解决方案1】:

尝试使用以下内容:

import numbers
products['test'] = pd.to_numeric(products['product_description_lenght'], errors='ignore').apply(lambda x: x + 1 if isinstance(x, numbers.Number) else x)

【讨论】:

  • 当我运行第一个命令时,我得到了这个错误:ValueError: could not convert string to float:
  • 当我运行最后一个命令时,我得到了 AttributeError: 'DataFrame' object has no attribute 'to_numeric'
  • @Lostsoul 使用pd.to_numeric 而不是df.to_numeric
  • @Lostsoul 尝试将我的代码复制到解释器并运行它
  • @U10-Forward-ReinstateMonica 我认为它有效!我会遍历它来检测我猜的数字吗?
猜你喜欢
  • 1970-01-01
  • 2020-12-21
  • 2018-11-06
  • 1970-01-01
  • 2022-01-16
  • 2020-12-16
  • 2023-03-22
  • 1970-01-01
相关资源
最近更新 更多