decode pandas data frame with sklearnHow to join (merge) data frames (inner, outer, left, right)Drop data frame columns by nameValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()Renaming columns in pandasDelete column from pandas DataFrame by column nameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasPython Pandas add column for row-wise max value of selected columnsNumPy creation by fromfunction errorCheck if string is in a pandas dataframe
How do I create uniquely male characters?
Why can't I see bouncing of a switch on an oscilloscope?
Writing rule stating superpower from different root cause is bad writing
Can divisibility rules for digits be generalized to sum of digits
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
Minkowski space
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
What does CI-V stand for?
How does one intimidate enemies without having the capacity for violence?
Can I make popcorn with any corn?
Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)
Why doesn't H₄O²⁺ exist?
Has the BBC provided arguments for saying Brexit being cancelled is unlikely?
What are these boxed doors outside store fronts in New York?
Why are 150k or 200k jobs considered good when there are 300k+ births a month?
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Email Account under attack (really) - anything I can do?
The Two and the One
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
can i play a electric guitar through a bass amp?
How old can references or sources in a thesis be?
What typically incentivizes a professor to change jobs to a lower ranking university?
Mathematical cryptic clues
What is the word for reserving something for yourself before others do?
decode pandas data frame with sklearn
How to join (merge) data frames (inner, outer, left, right)Drop data frame columns by nameValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()Renaming columns in pandasDelete column from pandas DataFrame by column nameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasPython Pandas add column for row-wise max value of selected columnsNumPy creation by fromfunction errorCheck if string is in a pandas dataframe
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a data frame with many columns. some of them are string and some other are integer.
I used this code to encode my data frame:
le = LabelEncoder()
for col in df.columns:
df_encoded[col] = df.apply(le.fit_transform)
it worked! but when I want to decode it with this code:
for col in df.columns:
df_decoded[col] = df_encoded.apply(le.inverse_transform)
I receive this error:
ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index MYCOLUMNNAME')
python dataframe sklearn-pandas
add a comment |
I have a data frame with many columns. some of them are string and some other are integer.
I used this code to encode my data frame:
le = LabelEncoder()
for col in df.columns:
df_encoded[col] = df.apply(le.fit_transform)
it worked! but when I want to decode it with this code:
for col in df.columns:
df_decoded[col] = df_encoded.apply(le.inverse_transform)
I receive this error:
ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index MYCOLUMNNAME')
python dataframe sklearn-pandas
provide complete error message and sample df too
– AkshayNevrekar
Mar 8 at 4:37
This is the complete error. I just removed the name of column for not making confusion
– CFD
Mar 8 at 6:02
add a comment |
I have a data frame with many columns. some of them are string and some other are integer.
I used this code to encode my data frame:
le = LabelEncoder()
for col in df.columns:
df_encoded[col] = df.apply(le.fit_transform)
it worked! but when I want to decode it with this code:
for col in df.columns:
df_decoded[col] = df_encoded.apply(le.inverse_transform)
I receive this error:
ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index MYCOLUMNNAME')
python dataframe sklearn-pandas
I have a data frame with many columns. some of them are string and some other are integer.
I used this code to encode my data frame:
le = LabelEncoder()
for col in df.columns:
df_encoded[col] = df.apply(le.fit_transform)
it worked! but when I want to decode it with this code:
for col in df.columns:
df_decoded[col] = df_encoded.apply(le.inverse_transform)
I receive this error:
ValueError: ('The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()', 'occurred at index MYCOLUMNNAME')
python dataframe sklearn-pandas
python dataframe sklearn-pandas
asked Mar 8 at 4:34
CFDCFD
967
967
provide complete error message and sample df too
– AkshayNevrekar
Mar 8 at 4:37
This is the complete error. I just removed the name of column for not making confusion
– CFD
Mar 8 at 6:02
add a comment |
provide complete error message and sample df too
– AkshayNevrekar
Mar 8 at 4:37
This is the complete error. I just removed the name of column for not making confusion
– CFD
Mar 8 at 6:02
provide complete error message and sample df too
– AkshayNevrekar
Mar 8 at 4:37
provide complete error message and sample df too
– AkshayNevrekar
Mar 8 at 4:37
This is the complete error. I just removed the name of column for not making confusion
– CFD
Mar 8 at 6:02
This is the complete error. I just removed the name of column for not making confusion
– CFD
Mar 8 at 6:02
add a comment |
1 Answer
1
active
oldest
votes
The type of data differs from column to column, so using apply
with fit_transform
won't work here. It will seem to work properly but the LabelEncoder
will be fitted to the rightmost column at the end of the operation, so when you'll try to apply the inverse_transform
, the LabelEncoder will replace all the elements in the other columns with the ones it saw in the rightmost column. E.g.:
df = pd.DataFrame(['A': 1, 'B': 'p', 'A': 1, 'B': 'q', 'A': 2, 'B': 'o', 'A': 3, 'B': 'p'])
df
A B
0 1 p
1 1 q
2 2 o
3 3 p
df = df.apply(le.fit_transform)
df
A B
0 0 1
1 0 2
2 1 0
3 2 1 # Looks fine
df.apply(le.inverse_transform)
A B
0 o p
1 o q
2 p o
3 q p # Whoops
You will see the same result even if you iterate over the columns one by one and perform the fit_transform
and inverse_transform
.
You need to fit the encoder to the correct column before inversing:
le = LabelEncoder()
df_encoded = pd.DataFrame(columns=df.columns)
df_decoded = pd.DataFrame(columns=df.columns)
for col in df.columns:
df_encoded[col] = le.fit_transform(df[col])
df_encoded
A B
0 0 1
1 0 2
2 1 0
3 2 1
for col in df.columns:
le = le.fit(df[col])
df_decoded[col] = le.inverse_transform(df_encoded[col])
df_decoded
A B
0 1 p
1 1 q
2 2 o
3 3 p # Yeay
Thanks for your answer. It worked! ...another question...What if we have something like this "P,Q'' in one of our cells in column B? I want to encode P and Q separately...I think it is a list inside the cell
– CFD
Mar 8 at 16:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056786%2fdecode-pandas-data-frame-with-sklearn%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The type of data differs from column to column, so using apply
with fit_transform
won't work here. It will seem to work properly but the LabelEncoder
will be fitted to the rightmost column at the end of the operation, so when you'll try to apply the inverse_transform
, the LabelEncoder will replace all the elements in the other columns with the ones it saw in the rightmost column. E.g.:
df = pd.DataFrame(['A': 1, 'B': 'p', 'A': 1, 'B': 'q', 'A': 2, 'B': 'o', 'A': 3, 'B': 'p'])
df
A B
0 1 p
1 1 q
2 2 o
3 3 p
df = df.apply(le.fit_transform)
df
A B
0 0 1
1 0 2
2 1 0
3 2 1 # Looks fine
df.apply(le.inverse_transform)
A B
0 o p
1 o q
2 p o
3 q p # Whoops
You will see the same result even if you iterate over the columns one by one and perform the fit_transform
and inverse_transform
.
You need to fit the encoder to the correct column before inversing:
le = LabelEncoder()
df_encoded = pd.DataFrame(columns=df.columns)
df_decoded = pd.DataFrame(columns=df.columns)
for col in df.columns:
df_encoded[col] = le.fit_transform(df[col])
df_encoded
A B
0 0 1
1 0 2
2 1 0
3 2 1
for col in df.columns:
le = le.fit(df[col])
df_decoded[col] = le.inverse_transform(df_encoded[col])
df_decoded
A B
0 1 p
1 1 q
2 2 o
3 3 p # Yeay
Thanks for your answer. It worked! ...another question...What if we have something like this "P,Q'' in one of our cells in column B? I want to encode P and Q separately...I think it is a list inside the cell
– CFD
Mar 8 at 16:05
add a comment |
The type of data differs from column to column, so using apply
with fit_transform
won't work here. It will seem to work properly but the LabelEncoder
will be fitted to the rightmost column at the end of the operation, so when you'll try to apply the inverse_transform
, the LabelEncoder will replace all the elements in the other columns with the ones it saw in the rightmost column. E.g.:
df = pd.DataFrame(['A': 1, 'B': 'p', 'A': 1, 'B': 'q', 'A': 2, 'B': 'o', 'A': 3, 'B': 'p'])
df
A B
0 1 p
1 1 q
2 2 o
3 3 p
df = df.apply(le.fit_transform)
df
A B
0 0 1
1 0 2
2 1 0
3 2 1 # Looks fine
df.apply(le.inverse_transform)
A B
0 o p
1 o q
2 p o
3 q p # Whoops
You will see the same result even if you iterate over the columns one by one and perform the fit_transform
and inverse_transform
.
You need to fit the encoder to the correct column before inversing:
le = LabelEncoder()
df_encoded = pd.DataFrame(columns=df.columns)
df_decoded = pd.DataFrame(columns=df.columns)
for col in df.columns:
df_encoded[col] = le.fit_transform(df[col])
df_encoded
A B
0 0 1
1 0 2
2 1 0
3 2 1
for col in df.columns:
le = le.fit(df[col])
df_decoded[col] = le.inverse_transform(df_encoded[col])
df_decoded
A B
0 1 p
1 1 q
2 2 o
3 3 p # Yeay
Thanks for your answer. It worked! ...another question...What if we have something like this "P,Q'' in one of our cells in column B? I want to encode P and Q separately...I think it is a list inside the cell
– CFD
Mar 8 at 16:05
add a comment |
The type of data differs from column to column, so using apply
with fit_transform
won't work here. It will seem to work properly but the LabelEncoder
will be fitted to the rightmost column at the end of the operation, so when you'll try to apply the inverse_transform
, the LabelEncoder will replace all the elements in the other columns with the ones it saw in the rightmost column. E.g.:
df = pd.DataFrame(['A': 1, 'B': 'p', 'A': 1, 'B': 'q', 'A': 2, 'B': 'o', 'A': 3, 'B': 'p'])
df
A B
0 1 p
1 1 q
2 2 o
3 3 p
df = df.apply(le.fit_transform)
df
A B
0 0 1
1 0 2
2 1 0
3 2 1 # Looks fine
df.apply(le.inverse_transform)
A B
0 o p
1 o q
2 p o
3 q p # Whoops
You will see the same result even if you iterate over the columns one by one and perform the fit_transform
and inverse_transform
.
You need to fit the encoder to the correct column before inversing:
le = LabelEncoder()
df_encoded = pd.DataFrame(columns=df.columns)
df_decoded = pd.DataFrame(columns=df.columns)
for col in df.columns:
df_encoded[col] = le.fit_transform(df[col])
df_encoded
A B
0 0 1
1 0 2
2 1 0
3 2 1
for col in df.columns:
le = le.fit(df[col])
df_decoded[col] = le.inverse_transform(df_encoded[col])
df_decoded
A B
0 1 p
1 1 q
2 2 o
3 3 p # Yeay
The type of data differs from column to column, so using apply
with fit_transform
won't work here. It will seem to work properly but the LabelEncoder
will be fitted to the rightmost column at the end of the operation, so when you'll try to apply the inverse_transform
, the LabelEncoder will replace all the elements in the other columns with the ones it saw in the rightmost column. E.g.:
df = pd.DataFrame(['A': 1, 'B': 'p', 'A': 1, 'B': 'q', 'A': 2, 'B': 'o', 'A': 3, 'B': 'p'])
df
A B
0 1 p
1 1 q
2 2 o
3 3 p
df = df.apply(le.fit_transform)
df
A B
0 0 1
1 0 2
2 1 0
3 2 1 # Looks fine
df.apply(le.inverse_transform)
A B
0 o p
1 o q
2 p o
3 q p # Whoops
You will see the same result even if you iterate over the columns one by one and perform the fit_transform
and inverse_transform
.
You need to fit the encoder to the correct column before inversing:
le = LabelEncoder()
df_encoded = pd.DataFrame(columns=df.columns)
df_decoded = pd.DataFrame(columns=df.columns)
for col in df.columns:
df_encoded[col] = le.fit_transform(df[col])
df_encoded
A B
0 0 1
1 0 2
2 1 0
3 2 1
for col in df.columns:
le = le.fit(df[col])
df_decoded[col] = le.inverse_transform(df_encoded[col])
df_decoded
A B
0 1 p
1 1 q
2 2 o
3 3 p # Yeay
edited Mar 8 at 10:38
answered Mar 8 at 7:46
entropyentropy
678214
678214
Thanks for your answer. It worked! ...another question...What if we have something like this "P,Q'' in one of our cells in column B? I want to encode P and Q separately...I think it is a list inside the cell
– CFD
Mar 8 at 16:05
add a comment |
Thanks for your answer. It worked! ...another question...What if we have something like this "P,Q'' in one of our cells in column B? I want to encode P and Q separately...I think it is a list inside the cell
– CFD
Mar 8 at 16:05
Thanks for your answer. It worked! ...another question...What if we have something like this "P,Q'' in one of our cells in column B? I want to encode P and Q separately...I think it is a list inside the cell
– CFD
Mar 8 at 16:05
Thanks for your answer. It worked! ...another question...What if we have something like this "P,Q'' in one of our cells in column B? I want to encode P and Q separately...I think it is a list inside the cell
– CFD
Mar 8 at 16:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55056786%2fdecode-pandas-data-frame-with-sklearn%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
provide complete error message and sample df too
– AkshayNevrekar
Mar 8 at 4:37
This is the complete error. I just removed the name of column for not making confusion
– CFD
Mar 8 at 6:02