Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何填写;B栏“;基于“中的价值”;A栏“;列在python中何时具有对象数据类型?_Python_Csv_Pandas_Numpy - Fatal编程技术网

如何填写;B栏“;基于“中的价值”;A栏“;列在python中何时具有对象数据类型?

如何填写;B栏“;基于“中的价值”;A栏“;列在python中何时具有对象数据类型?,python,csv,pandas,numpy,Python,Csv,Pandas,Numpy,我有一个CSV文件,作为数据帧导入。 我想根据另一列中的某些特定术语创建并填充一列。 包含所有这些值的列是对象数据类型。它有如下价值: ABC|MNO - 2017 - Trial|1|Random|xyz|RUN|Google|1x1|A10001-21|SD|GH|PRIME - 2017 - Big - This is For Example 我使用的代码是: def new(row): if row.str.contains("PRIME"): return 'A'

我有一个CSV文件,作为数据帧导入。 我想根据另一列中的某些特定术语创建并填充一列。 包含所有这些值的列是对象数据类型。它有如下价值:

ABC|MNO - 2017 - Trial|1|Random|xyz|RUN|Google|1x1|A10001-21|SD|GH|PRIME - 2017 - Big - This is For Example 
我使用的代码是:

def new(row):
  if row.str.contains("PRIME"):
      return 'A'
  if row.str.contains("Random"):
      return 'B'
  if row.str.contains("Google"):
      return 'C'

df['X'] = df['Y'].apply (lambda row: new (row))
此代码给了我以下错误:

AttributeError: 'str' object has no attribute 'str'
我认为这是因为列X具有对象数据类型

我尝试使用以下代码将其转换为字符串:

df['Y'] = df['Y'].astype('str')
但它不起作用。然后,我尝试使用以下代码拆分它:

df['Y_new'] = df['Y'].str.split(r'([A-Z][^\.!?]*[\.!?])')
但它将所有值转换为NaN。我应该怎么做呢?

试着这样做:

    def new(row):
      if row.contains("PRIME"):
         return 'A'
      if row.contains("Random"):
         return 'B'
      if row.contains("Google"):
         return 'C'

确保您了解函数中的
。如有必要,在函数中添加
print(type(row))
语句。如果
df['Y']
是对象数据类型,则
行的类型在每次调用时可能不同。已尝试,但它返回
AttributeError:'str'对象没有属性'contains'