How to change data type of values in pandasRenaming columns in pandasNumPy or Pandas: Keeping array type as integer while having a NaN valueFilter dataframe rows if value in column is in a set list of valuesUse a list of values to select rows from a pandas dataframeAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframe“Large data” work flows using pandasChange data type of columns in PandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandas

How can I make sure my players' decisions have consequences?

What rules turn any attack that hits a given target into a critical hit?

Short story about a group of sci-fi writers sitting around discussing their profession

Travelling from Venice to Budapest, making a stop in Croatia

what to say when a company asks you why someone (a friend) who was fired left?

How to correct errors in proofs of an accepted paper

Why do people say "I am broke" instead of "I am broken"?

Does Impedance Matching Imply any Practical RF Transmitter Must Waste >=50% of Energy?

From the start of the game what is the longest possible series of consecutive white moves where white can do those moves no matter what black does?

If a check is written for bill, but account number is not mentioned on memo line, is it still processed?

Download file from URL to Safehouse

Considerations when providing money to one child now, and the other later?

What is an Eternal Word™?

Can I pay with HKD in Macau or Shenzhen?

ExactlyOne extension method

How to repair basic cable/wire issue for household appliances

What was the rationale behind 36 bit computer architectures?

How to make this script shorter?

Killing a star safely

What is the best word describing the nature of expiring in a short amount of time, connoting "losing public attention"?

Why did modems have speakers?

What's the 1 inch size square knob sticking out of wall?

A team with high solidarity

What is "ass door"?



How to change data type of values in pandas


Renaming columns in pandasNumPy or Pandas: Keeping array type as integer while having a NaN valueFilter dataframe rows if value in column is in a set list of valuesUse a list of values to select rows from a pandas dataframeAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframe“Large data” work flows using pandasChange data type of columns in PandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandas






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a pandas dataframe that has both floats and numpy arrays as follows.



 name x1 x2 X3
0 a 4.7 [0.] 3
1 b 3.2 [1.7] 5
2 e 1.2 [2.] 6


I want to change all the numpy values in the dataframe into floats.
i.e.



 name x1 x2 X3
0 a 4.7 0. 3
1 b 3.2 1.7 5
2 e 1.2 2. 6


Can we do that using pandas?



I am happy to provide more details if needed.










share|improve this question






















  • There are only one element lists?

    – jezrael
    Mar 26 at 14:28

















1















I have a pandas dataframe that has both floats and numpy arrays as follows.



 name x1 x2 X3
0 a 4.7 [0.] 3
1 b 3.2 [1.7] 5
2 e 1.2 [2.] 6


I want to change all the numpy values in the dataframe into floats.
i.e.



 name x1 x2 X3
0 a 4.7 0. 3
1 b 3.2 1.7 5
2 e 1.2 2. 6


Can we do that using pandas?



I am happy to provide more details if needed.










share|improve this question






















  • There are only one element lists?

    – jezrael
    Mar 26 at 14:28













1












1








1








I have a pandas dataframe that has both floats and numpy arrays as follows.



 name x1 x2 X3
0 a 4.7 [0.] 3
1 b 3.2 [1.7] 5
2 e 1.2 [2.] 6


I want to change all the numpy values in the dataframe into floats.
i.e.



 name x1 x2 X3
0 a 4.7 0. 3
1 b 3.2 1.7 5
2 e 1.2 2. 6


Can we do that using pandas?



I am happy to provide more details if needed.










share|improve this question














I have a pandas dataframe that has both floats and numpy arrays as follows.



 name x1 x2 X3
0 a 4.7 [0.] 3
1 b 3.2 [1.7] 5
2 e 1.2 [2.] 6


I want to change all the numpy values in the dataframe into floats.
i.e.



 name x1 x2 X3
0 a 4.7 0. 3
1 b 3.2 1.7 5
2 e 1.2 2. 6


Can we do that using pandas?



I am happy to provide more details if needed.







pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 14:27









EmJEmJ

7745 silver badges25 bronze badges




7745 silver badges25 bronze badges












  • There are only one element lists?

    – jezrael
    Mar 26 at 14:28

















  • There are only one element lists?

    – jezrael
    Mar 26 at 14:28
















There are only one element lists?

– jezrael
Mar 26 at 14:28





There are only one element lists?

– jezrael
Mar 26 at 14:28












1 Answer
1






active

oldest

votes


















1














If only one element lists use:



df['x2'] = df['x2'].str[0]


Or:



df['x2'] = df['x2'].apply(lambda x: x[0])


EDIT:



After comments there are strings in columns, so need:



mask = df.iloc[0].astype(str).str.startswith('[')

df.loc[:, mask] = df.loc[:, mask].apply(lambda x: x.str.strip('[]')).astype(float)





share|improve this answer




















  • 1





    Hi, thanks a lot for the great answer. However, in my real dataset, I do not know what column names have numpy. Is there any way to do this without specifically mentioning the column name? :)

    – EmJ
    Mar 26 at 14:33







  • 1





    @Emi - S there are only one element lists?

    – jezrael
    Mar 26 at 14:34






  • 1





    @Emi - Or one element arrays in column, or floats ?

    – jezrael
    Mar 26 at 14:35











  • Not sure what you meant. There are multiple columns (say 25 columns) and about 15 of the columns have numpy values :) Please let me know if you want an example. I can update the question.

    – EmJ
    Mar 26 at 14:37







  • 1





    @Emi - ya, I think if is possible some value is [0,2.1] - length 2 or all arrays have only one value - [0.]

    – jezrael
    Mar 26 at 14:38










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%2f55359566%2fhow-to-change-data-type-of-values-in-pandas%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














If only one element lists use:



df['x2'] = df['x2'].str[0]


Or:



df['x2'] = df['x2'].apply(lambda x: x[0])


EDIT:



After comments there are strings in columns, so need:



mask = df.iloc[0].astype(str).str.startswith('[')

df.loc[:, mask] = df.loc[:, mask].apply(lambda x: x.str.strip('[]')).astype(float)





share|improve this answer




















  • 1





    Hi, thanks a lot for the great answer. However, in my real dataset, I do not know what column names have numpy. Is there any way to do this without specifically mentioning the column name? :)

    – EmJ
    Mar 26 at 14:33







  • 1





    @Emi - S there are only one element lists?

    – jezrael
    Mar 26 at 14:34






  • 1





    @Emi - Or one element arrays in column, or floats ?

    – jezrael
    Mar 26 at 14:35











  • Not sure what you meant. There are multiple columns (say 25 columns) and about 15 of the columns have numpy values :) Please let me know if you want an example. I can update the question.

    – EmJ
    Mar 26 at 14:37







  • 1





    @Emi - ya, I think if is possible some value is [0,2.1] - length 2 or all arrays have only one value - [0.]

    – jezrael
    Mar 26 at 14:38















1














If only one element lists use:



df['x2'] = df['x2'].str[0]


Or:



df['x2'] = df['x2'].apply(lambda x: x[0])


EDIT:



After comments there are strings in columns, so need:



mask = df.iloc[0].astype(str).str.startswith('[')

df.loc[:, mask] = df.loc[:, mask].apply(lambda x: x.str.strip('[]')).astype(float)





share|improve this answer




















  • 1





    Hi, thanks a lot for the great answer. However, in my real dataset, I do not know what column names have numpy. Is there any way to do this without specifically mentioning the column name? :)

    – EmJ
    Mar 26 at 14:33







  • 1





    @Emi - S there are only one element lists?

    – jezrael
    Mar 26 at 14:34






  • 1





    @Emi - Or one element arrays in column, or floats ?

    – jezrael
    Mar 26 at 14:35











  • Not sure what you meant. There are multiple columns (say 25 columns) and about 15 of the columns have numpy values :) Please let me know if you want an example. I can update the question.

    – EmJ
    Mar 26 at 14:37







  • 1





    @Emi - ya, I think if is possible some value is [0,2.1] - length 2 or all arrays have only one value - [0.]

    – jezrael
    Mar 26 at 14:38













1












1








1







If only one element lists use:



df['x2'] = df['x2'].str[0]


Or:



df['x2'] = df['x2'].apply(lambda x: x[0])


EDIT:



After comments there are strings in columns, so need:



mask = df.iloc[0].astype(str).str.startswith('[')

df.loc[:, mask] = df.loc[:, mask].apply(lambda x: x.str.strip('[]')).astype(float)





share|improve this answer















If only one element lists use:



df['x2'] = df['x2'].str[0]


Or:



df['x2'] = df['x2'].apply(lambda x: x[0])


EDIT:



After comments there are strings in columns, so need:



mask = df.iloc[0].astype(str).str.startswith('[')

df.loc[:, mask] = df.loc[:, mask].apply(lambda x: x.str.strip('[]')).astype(float)






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 15:19

























answered Mar 26 at 14:28









jezraeljezrael

392k28 gold badges402 silver badges475 bronze badges




392k28 gold badges402 silver badges475 bronze badges







  • 1





    Hi, thanks a lot for the great answer. However, in my real dataset, I do not know what column names have numpy. Is there any way to do this without specifically mentioning the column name? :)

    – EmJ
    Mar 26 at 14:33







  • 1





    @Emi - S there are only one element lists?

    – jezrael
    Mar 26 at 14:34






  • 1





    @Emi - Or one element arrays in column, or floats ?

    – jezrael
    Mar 26 at 14:35











  • Not sure what you meant. There are multiple columns (say 25 columns) and about 15 of the columns have numpy values :) Please let me know if you want an example. I can update the question.

    – EmJ
    Mar 26 at 14:37







  • 1





    @Emi - ya, I think if is possible some value is [0,2.1] - length 2 or all arrays have only one value - [0.]

    – jezrael
    Mar 26 at 14:38












  • 1





    Hi, thanks a lot for the great answer. However, in my real dataset, I do not know what column names have numpy. Is there any way to do this without specifically mentioning the column name? :)

    – EmJ
    Mar 26 at 14:33







  • 1





    @Emi - S there are only one element lists?

    – jezrael
    Mar 26 at 14:34






  • 1





    @Emi - Or one element arrays in column, or floats ?

    – jezrael
    Mar 26 at 14:35











  • Not sure what you meant. There are multiple columns (say 25 columns) and about 15 of the columns have numpy values :) Please let me know if you want an example. I can update the question.

    – EmJ
    Mar 26 at 14:37







  • 1





    @Emi - ya, I think if is possible some value is [0,2.1] - length 2 or all arrays have only one value - [0.]

    – jezrael
    Mar 26 at 14:38







1




1





Hi, thanks a lot for the great answer. However, in my real dataset, I do not know what column names have numpy. Is there any way to do this without specifically mentioning the column name? :)

– EmJ
Mar 26 at 14:33






Hi, thanks a lot for the great answer. However, in my real dataset, I do not know what column names have numpy. Is there any way to do this without specifically mentioning the column name? :)

– EmJ
Mar 26 at 14:33





1




1





@Emi - S there are only one element lists?

– jezrael
Mar 26 at 14:34





@Emi - S there are only one element lists?

– jezrael
Mar 26 at 14:34




1




1





@Emi - Or one element arrays in column, or floats ?

– jezrael
Mar 26 at 14:35





@Emi - Or one element arrays in column, or floats ?

– jezrael
Mar 26 at 14:35













Not sure what you meant. There are multiple columns (say 25 columns) and about 15 of the columns have numpy values :) Please let me know if you want an example. I can update the question.

– EmJ
Mar 26 at 14:37






Not sure what you meant. There are multiple columns (say 25 columns) and about 15 of the columns have numpy values :) Please let me know if you want an example. I can update the question.

– EmJ
Mar 26 at 14:37





1




1





@Emi - ya, I think if is possible some value is [0,2.1] - length 2 or all arrays have only one value - [0.]

– jezrael
Mar 26 at 14:38





@Emi - ya, I think if is possible some value is [0,2.1] - length 2 or all arrays have only one value - [0.]

– jezrael
Mar 26 at 14:38








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















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%2f55359566%2fhow-to-change-data-type-of-values-in-pandas%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript