简体   繁体   中英

Using .str on Pandas Series converts all data to NaN float64 type

I'm trying to use .str on my Pandas series in order to use the string operator methods but the.str converts all my data to NaN of the float64 dtype. The Pandas series is an object dtype to begin with.

Below I show my API series and then try to perform the split method on it.

In [80]:
wellinfo['API'].head()

Out[80]:

 0    3501124153
 1    3501124154
 2    3501124155
 3    3501124185
 4    3501725290
 Name: API, dtype: object

In [81]: 
wellinfo['API'].str.split("0")

Out[81]:
0      NaN
1      NaN
2      NaN
3      NaN
4      NaN
5      NaN
...
1537   NaN
1538   NaN
1539   NaN
1540   NaN
1541   NaN
1542   NaN
Name: API, Length: 1543, dtype: float64

I've skimmed through the Pandas documentation but cannot find out why it is converting everything. I've also tried multiple methods besides the split method with the same results.

Any information is appreciated. Thank you.

simply select the str values and do the operation in this case replace. On the other values that are not str returns NaN. Use Series.fillna :

wellinfo['API'].str.split("0").fillna(wellinfo['API'])

or to modify int values:

wellinfo['API'].astype(str).str.split("0")

as suggested @Mstaino

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