DataFrame.astype()

Pandas

Cast dtype to a perticular dtype with options.
Let us change one type to other. Here column MATH is changed to string and '2' ( '2' dtpye is string not integer ) is added.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data['MATH'].astype(str)+'2')
Output
0    802
1    402
2    702
3    702
4    702
5    302
Let us change the last line only.
print(my_data['MATH']+2)
Output
0    82
1    42
2    72
3    72
4    72
5    32
Name: MATH, dtype: int64
Let us change the MATH column to int32
print(my_data['MATH'].astype('int32'))
Output
0    80
1    40
2    70
3    70
4    70
5    30
Name: MATH, dtype: int32
str.contains.sum()

Pandas read_csv() read_excel() to_excel()


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer