ValueError: 使用assign时无法在具有重复标签的轴上重新索引

huangapple go评论102阅读模式
英文:

ValueError: cannot reindex on an axis with duplicate labels while using assign

问题

I am trying to split the values inside the engine_type column using _ delimiter using the following code:

  1. df = pd.read_csv("/content/sample_data/used_cars.csv")
  2. dds = df.assign(engines_type= lambda x: x['engine_type'].str.split(r'\s*_\s*').explode()).reset_index()

I am getting the following error:

ValueError: cannot reindex on an axis with duplicate labels

What could be the reason for this error?

Thanks in advance.

英文:

I am trying to split the values inside the engine_type column using _ delimiter using the following code

  1. df = pd.read_csv("/content/sample_data/used_cars.csv")
  2. dds = df.assign(engines_type= lambda x: x['engine_type'].str.split(r'\s*_\s*').explode()).reset_index()

I am getting the following error

> ValueError: cannot reindex on an axis with duplicate labels

What could be the reason for this error?

Thanks in advance

答案1

得分: 0

尝试这种方法:

  1. # 假设您有一个像这样的数据框:
  2. df = pd.DataFrame({
  3. 'car_model': ['Renualt', 'Hyundai', 'Ford'],
  4. 'engine_type': ['Gas', 'Diesel_Petrol', 'Gas_Hybrid']
  5. })
  6. dds = (df.assign(engine_type=df['engine_type'].str.split(r'\s*_\s*'))
  7. .explode('engine_type')
  8. .reset_index(drop=True)
  9. )
  10. print(dds)

  1. car_model engine_type
  2. 0 Renualt Gas
  3. 1 Hyundai Diesel
  4. 2 Hyundai Petrol
  5. 3 Ford Gas
  6. 4 Ford Hybrid

注意: 如果这不适用,您应该提供一个示例数据框和期望的输出。

英文:

Try this approach:<br>

  1. # Say you have a df like:
  2. df = pd.DataFrame({
  3. &#39;car_model&#39;: [&#39;Renualt&#39;, &#39;Hyundai&#39;, &#39;Ford&#39;],
  4. &#39;engine_type&#39;: [&#39;Gas&#39;, &#39;Diesel_Petrol&#39;, &#39;Gas_Hybrid&#39;]
  5. })
  6. dds = (df.assign(engine_type=df[&#39;engine_type&#39;].str.split(r&#39;\s*_\s*&#39;))
  7. .explode(&#39;engine_type&#39;)
  8. .reset_index(drop=True)
  9. )
  10. print(dds)

  1. car_model engine_type
  2. 0 Renualt Gas
  3. 1 Hyundai Diesel
  4. 2 Hyundai Petrol
  5. 3 Ford Gas
  6. 4 Ford Hybrid

Note: If this doesn't help, you should provide a sample dataframe and a desired output.

huangapple
  • 本文由 发表于 2023年4月11日 14:33:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定