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
Existing of non-intersecting rays
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
What was the exact wording from Ivanhoe of this advice on how to free yourself from slavery?
How to bake one texture for one mesh with multiple textures blender 2.8
Has any country ever had 2 former presidents in jail simultaneously?
How can "mimic phobia" be cured or prevented?
Why does the Sun have different day lengths, but not the gas giants?
Is this toilet slogan correct usage of the English language?
Yosemite Fire Rings - What to Expect?
2.8 Why are collections grayed out? How can I open them?
Is there a working SACD iso player for Ubuntu?
What is the evidence for the "tyranny of the majority problem" in a direct democracy context?
Delivering sarcasm
Strong empirical falsification of quantum mechanics based on vacuum energy density
Removing files under particular conditions (number of files, file age)
Should I outline or discovery write my stories?
Are paving bricks differently sized for sand bedding vs mortar bedding?
Lowest total scrabble score
Is the U.S. Code copyrighted by the Government?
Not using 's' for he/she/it
Problem with TransformedDistribution
Why electric field inside a cavity of a non-conducting sphere not zero?
Are the IPv6 address space and IPv4 address space completely disjoint?
Did arcade monitors have same pixel aspect ratio as TV sets?
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
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
966
966
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
suicidalteddysuicidalteddy
610114
610114
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