Comments (4)
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.
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.
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.
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)
- DOC: to_sql docs should mention ADBC HOT 1
- BUG: DataFrame.xs multi-index drop_level=False has no effect when level= is left at default
- DataFrameGroupBy.agg with nan results into inf HOT 1
- CI: Don't run the 'trailing-whitespace' check on markdown files.
- BUG: HDF support and `show_versions()` broken with pandas 2.2.2 and numpy 2.0
- BUG: to_sql does not populate index column with a value when using the mssql+pyodbc engine HOT 1
- BUG: to_sql does gives incorrect column name for index when callable passed in to method HOT 1
- ENH: Back pd.BooleanArray with nanoarrow HOT 1
- BUG: None becomes empty string when writing multiple columns to CSV, but double quotes "" when writing single columns HOT 2
- BUG: Unable to import `pandas` when `pyarrow` 16.1.0 is installed HOT 2
- BUG: datetime64[s] data changes when put into HDFStore HOT 1
- BUG: random crash / hang when calculating rolling sum HOT 2
- BUG: 0/0 with arrow backend is not "NA" HOT 3
- BUG: pivot_table chokes on pd.DatetimeTZDtype if there are no rows.
- DOC: "Accelerated operations" talks about speedup in obsolete versions of Pandas HOT 2
- BUG: rounding datetime in series is broken HOT 1
- BUG: DataFrame to JSON failed when it with UUID HOT 4
- ENH: merge_asof with "how” functionality
- ENH: is it possible to save a reference to a method depending on the version HOT 3
- ValueError: Usecols do not match columns, columns expected but not found: ['Col3', 'Col1'] HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pandas.