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)
- BUG: `Series.combine_first` replaces `None` with `NaN` at index not provided by `other` HOT 3
- BUG: in agg function HOT 1
- BUG: Rounding timedelta to 3 decimal places does not work correctly and is unstable. HOT 2
- BUG: pd.read_sql() converts string "NA" to None HOT 1
- API: timestamp resolution inference - default to one unit (if possible) instead of being data-dependent?
- DOC: punctuation of titles in Titanic data HOT 1
- BUG: `unstack` with `sort=False` scrambles data HOT 3
- BUG: pandas.read_parquet fails to read parquet files with "file://" prefix on Windows HOT 2
- BUG: csv date inconsistencies HOT 2
- Potential performance regression with "BUG: DateTimeIndex.is_year_start unexpected behavior when constructed…" HOT 1
- BUG: output of pandas.util.hash_pandas_object() changed in pandas 2.2.x HOT 2
- BUG: docker build on windows fails; need eol=lf in .gitattributes HOT 2
- BUG: None value in MultiIndex can't be found
- BUG: HDFStore doesn't save datetime64[s] right HOT 1
- ENH: Pivot Table HOT 1
- ENH: Pandas Tensor Data Type HOT 1
- BUG: Accessing a row with loc recasts dtype and can cause values to change depending on other columns HOT 2
- BUG: Reading large CSV files with `pyarrow` when values contain newline character. HOT 1
- DOC: Link to SPEC 0 instead of NEP 29 HOT 2
- BUG: `to_parquet(engine="fastparquet")` can't handle DataFrame with NumPy array values HOT 1
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.