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

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

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:

df = pd.read_csv("/content/sample_data/used_cars.csv")
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

df = pd.read_csv("/content/sample_data/used_cars.csv")
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

尝试这种方法:

# 假设您有一个像这样的数据框:
df = pd.DataFrame({
    'car_model': ['Renualt', 'Hyundai', 'Ford'],
    'engine_type': ['Gas', 'Diesel_Petrol', 'Gas_Hybrid']
})

dds = (df.assign(engine_type=df['engine_type'].str.split(r'\s*_\s*'))
         .explode('engine_type')
         .reset_index(drop=True)
    )
print(dds)

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

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

英文:

Try this approach:<br>

# Say you have a df like:
df = pd.DataFrame({
    &#39;car_model&#39;: [&#39;Renualt&#39;, &#39;Hyundai&#39;, &#39;Ford&#39;],
    &#39;engine_type&#39;: [&#39;Gas&#39;, &#39;Diesel_Petrol&#39;, &#39;Gas_Hybrid&#39;]
})

dds = (df.assign(engine_type=df[&#39;engine_type&#39;].str.split(r&#39;\s*_\s*&#39;))
         .explode(&#39;engine_type&#39;)
         .reset_index(drop=True)
    )
print(dds)

  car_model engine_type
0   Renualt         Gas
1   Hyundai      Diesel
2   Hyundai      Petrol
3      Ford         Gas
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:

确定