i am having one DataFrame as shown below,i want to convert the decimal number to integer without rounding.
Date A B
01-01-2022 12.87 0.67
02-01-2022 3.8976 123.98
03-01-2022 6.7654 23.98
04-01-2022 2.897 0.098
05-01-2022 3.986 0.76
06-01-2022 1.09 12.45
Desired output:
Date A B
01-01-2022 12 0
02-01-2022 3 123
03-01-2022 6 23
04-01-2022 2 0
05-01-2022 3 0
06-01-2022 1 12
CodePudding user response:
Option 1
One way to do that is to cast to the type int
as follows
df[['A', 'B']] = df[['A', 'B']].astype(int)
[Out]:
Date A B
0 01-01-2022 12 0
1 02-01-2022 3 123
2 03-01-2022 6 23
3 04-01-2022 2 0
4 05-01-2022 3 0
5 06-01-2022 1 12
Option 2
One can also do it with .applymap()
as follows
df[['A', 'B']] = df[['A', 'B']].applymap(int)
[Out]:
Date A B
0 01-01-2022 12 0
1 02-01-2022 3 123
2 03-01-2022 6 23
3 04-01-2022 2 0
4 05-01-2022 3 0
5 06-01-2022 1 12
Or using custom lambda functions
df[['A', 'B']] = df[['A', 'B']].applymap(lambda x: int(x))
[Out]:
Date A B
0 01-01-2022 12 0
1 02-01-2022 3 123
2 03-01-2022 6 23
3 04-01-2022 2 0
4 05-01-2022 3 0
5 06-01-2022 1 12
Option 3
Another way is using .apply()
with a custom lambda function
df[['A', 'B']] = df[['A', 'B']].apply(lambda x: x.astype(int))
[Out]:
Date A B
0 01-01-2022 12 0
1 02-01-2022 3 123
2 03-01-2022 6 23
3 04-01-2022 2 0
4 05-01-2022 3 0
5 06-01-2022 1 12
CodePudding user response:
In basic python we can use int(floating_n0) .
Example:
x= 3.8976
print(x)
print(int(x))
Output: 3.