Coder Social home page Coder Social logo

Comments (4)

luke396 avatar luke396 commented on June 15, 2024

Hi @lucasmouragomes, I was able to reproduce the example:

import pandas as pd
df = pd.DataFrame({'val':[0, 1, 2]})
df.set_index(['a', 'b', 'c'])
# KeyError: "None of ['a', 'b', 'c'] are in the columns"

df = pd.DataFrame({'val':[0, 1, 2]})
df.set_index(pd.Series(['a', 'b', 'c']))
#   val
# a  0
# b  1
# c  2

However, do you notice that the documentation states 'Set the DataFrame index using existing columns.'? I think the KeyError you mentioned is correct.

In my view, if you want to set a new index for the DataFrame, you could try:

pd.DataFrame({'val':[0, 1, 2]}, index=['a', 'b', 'c'])

This might work as expected.

from pandas.

Kapple14 avatar Kapple14 commented on June 15, 2024

I can fully understand the documentation and see why you have responded like this, but wouldn't it be nicer to just rename the indexes by default instead of throwing an error?

from pandas.

rajat315315 avatar rajat315315 commented on June 15, 2024

It's wierd to see that both of the following works:

pd.DataFrame({'val':[0, 1, 2]}).set_index(np.array(['a', 'b', 'c']))
pd.DataFrame({'val':[0, 1, 2]}).set_index([['a', 'b', 'c']])

As per the documentation, it need a a single array of the same length as the calling DataFrame as key, so maybe that is why np.array is working fine but list is not, but its unexpected.

from pandas.

Kapple14 avatar Kapple14 commented on June 15, 2024

Would someone else be interested in creating a bounty for the solving of the bounty? =>https://www.hydraoss.io/

Would be happy to contribute to it.

from pandas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.