casting

Change column type in pandas

Change column type in pandas Question: I created a DataFrame from a list of lists: table = [ [‘a’, ‘1.2’, ‘4.2’ ], [‘b’, ’70’, ‘0.03’], [‘x’, ‘5’, ‘0’ ], ] df = pd.DataFrame(table) How do I convert the columns to specific types? In this case, I want to convert columns 2 and 3 into floats. …

Total answers: 16

Import pandas dataframe column as string not int

Import pandas dataframe column as string not int Question: I would like to import the following csv as strings not as int64. Pandas read_csv automatically converts it to int64, but I need this column as string. ID 00013007854817840016671868 00013007854817840016749251 00013007854817840016754630 00013007854817840016781876 00013007854817840017028824 00013007854817840017963235 00013007854817840018860166 df = read_csv(‘sample.csv’) df.ID >> 0 -9223372036854775808 1 -9223372036854775808 2 -9223372036854775808 …

Total answers: 3

Lexical cast from string to type

Lexical cast from string to type Question: Recently, I was trying to store and read information from files in Python, and came across a slight problem: I wanted to read type information from text files. Type casting from string to int or to float is quite efficient, but type casting from string to type seems …

Total answers: 5

How do I concatenate a boolean to a string in Python?

How do I concatenate a boolean to a string in Python? Question: I want to accomplish the following answer = True myvar = “the answer is ” + answer and have myvar’s value be “the answer is True”. I’m pretty sure you can do this in Java. Asked By: travis1097 || Source Answers: answer = …

Total answers: 6

Python: Test if value can be converted to an int in a list comprehension

Python: Test if value can be converted to an int in a list comprehension Question: Basically I want to do this; return [ row for row in listOfLists if row[x] is int ] But row[x] is a text value that may or may not be convertible to an int I’m aware that this could be …

Total answers: 3

What's the logical value of "string" in Python?

What's the logical value of "string" in Python? Question: I erroneously wrote this code in Python: name = input(“what is your name?”) if name == “Kamran” or “Samaneh”: print(“That is a nice name”) else: print(“You have a boring name ;)”) It always prints out “That is a nice name” even when the input is neither …

Total answers: 6

overriding bool() for custom class

overriding bool() for custom class Question: All I want is for bool(myInstance) to return False (and for myInstance to evaluate to False when in a conditional like if/or/and. I know how to override >, <, =) I’ve tried this: class test: def __bool__(self): return False myInst = test() print bool(myInst) #prints “True” print myInst.__bool__() #prints …

Total answers: 6

What is the difference between the __int__ and __index__ methods in Python 3?

What is the difference between the __int__ and __index__ methods in Python 3? Question: The Data Model section of the Python 3.2 documentation provides the following descriptions for the __int__ and __index__ methods: object.__int__(self) Called to implement the built-in [function int()]. Should return [an integer]. object.__index__(self) Called to implement operator.index(). Also called whenever Python needs …

Total answers: 2

In Python, how do I convert all of the items in a list to floats?

In Python, how do I convert all of the items in a list to floats? Question: I have a script which reads a text file, pulls decimal numbers out of it as strings and places them into a list. So I have this list: my_list = [‘0.49’, ‘0.54’, ‘0.54’, ‘0.55’, ‘0.55’, ‘0.54’, ‘0.55’, ‘0.55’, ‘0.54’] …

Total answers: 13

What's the difference between casting and coercion in Python?

What's the difference between casting and coercion in Python? Question: In the Python documentation and on mailing lists I see that values are sometimes "cast", and sometimes "coerced". Asked By: Justin R. || Source Answers: I think “casting” shouldn’t be used for Python; there are only type conversion, but no casts (in the C sense). …

Total answers: 2