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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page