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













2















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')









share|improve this question






















  • 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















2















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')









share|improve this question






















  • 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













2












2








2








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')









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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

















  • 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












1 Answer
1






active

oldest

votes


















1














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





share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1














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





share|improve this answer

























  • 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















1














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





share|improve this answer

























  • 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













1












1








1







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme