Skip to content

Instantly share code, notes, and snippets.

@ZeccaLehn
Last active June 28, 2016 21:59
Show Gist options
  • Save ZeccaLehn/6f1c34efebc44c92282eab3e8cdfb7b8 to your computer and use it in GitHub Desktop.
Save ZeccaLehn/6f1c34efebc44c92282eab3e8cdfb7b8 to your computer and use it in GitHub Desktop.
Python Change Pandas DataFrame Variables: String to Float
# Python Change Pandas DataFrame Variables

import pandas as pd

data = [['hello', '1.222', '4', '5.22'], ['world', '66', '4', '6.33'], ['hello', '4.22', '0', '5.24']]
data = pd.DataFrame(data, columns = ['a', 'b', 'c', 'd'])
data
a b c d
0 hello 1.222 4 5.22
1 world 66 4 6.33
2 hello 4.22 0 5.24
data.dtypes
a    object
b    object
c    object
d    object
dtype: object
data.a[0] + " "  + data.a[1]
'hello world'
data[['b', 'd']] = data[['b', 'd']].astype(float)
data[['c']] = data[['c']].astype(int)
data[['a']] = data[['a']].astype(str) # Defaults to string
data
a b c d
0 hello 1.222 4 5.22
1 world 66.000 4 6.33
2 hello 4.220 0 5.24
data.dtypes
a     object
b    float64
c      int32
d    float64
dtype: object
data.a[0] + " "  + data.a[1]
'hello world'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment