-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataframe_column_type_conversion.py
46 lines (34 loc) · 1.53 KB
/
dataframe_column_type_conversion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
try:
r = requests.get('http://www.google.com/nothere')
r.raise_for_status()
except requests.exceptions.HTTPError as err:
raise SystemExit(err)
Will print:
404 Client Error: Not Found for url: http://www.google.com/nothere
====================================================
print(df.loc[df['A'].str.contains('A4'),'C'].item())
====================================================
dataTypeSeries = combined_output.dtypes
print('Data type of each column of combined_output Dataframe :')
print(dataTypeSeries)
# https://stackoverflow.com/questions/15891038/change-data-type-of-columns-in-pandas
# you could do something like the following to downcast integer columns
# to the smallest (integer) dtype that will hold the values:
df.apply(pd.to_numeric, downcast="integer", errors="ignore")
# As long as your values can all be converted, here is all you need:
# convert column "a" of a DataFrame
df["a"] = pd.to_numeric(df["a"])
# convert all columns of DataFrame
df = df.apply(pd.to_numeric) # convert all columns of DataFrame
# convert just columns "a" and "b"
df[["a", "b"]] = df[["a", "b"]].apply(pd.to_numeric)
df["y"] = pd.to_numeric(df["y"])
df["z"] = pd.to_datetime(df["z"])
# ==================================
#The only way I can set them is by looping through each column variable and recasting with astype.
dtypes = {'x':'object','y':'int'}
mydata = pd.DataFrame([['a','1'],['b','2']],
columns=['x','y'])
for c in mydata.columns:
mydata[c] = mydata[c].astype(dtypes[c])
print mydata['y'].dtype #=> int64