简体   繁体   中英

Why pandas column not converted to Int64?

Here is my code:

import pandas as pd
import numpy as np

data = {'ID': [1,2,3,4,5,6,7,8,9],
       'Doc':['Order','Order','Inv','Order','Order','Shp','Order', 'Order','Inv'],
       'Rep':[101,101,101,102,102,102,103,103,103]}
df = pd.DataFrame(data)


#df['concat']= (df['ID'].convert_dtypes(convert_string = True) + "1").convert_dtypes(convert_integer = True)

df['concat']= (df['ID'].astype(str) + "1").convert_dtypes(convert_integer = True)

print(df['concat'].dtype)

output:

string

Why df['concat'] is not Int64 and how to get it?

Try

>>> df['concat'] = df['ID'].astype(str).add('1').astype('int64')
0    11
1    21
2    31
3    41
4    51
5    61
6    71
7    81
8    91
Name: ID, dtype: int64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM