Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查熊猫系列的数据类型&;将字符串转换为数字_Python_Pandas_Dataframe_Time Series - Fatal编程技术网

Python 检查熊猫系列的数据类型&;将字符串转换为数字

Python 检查熊猫系列的数据类型&;将字符串转换为数字,python,pandas,dataframe,time-series,Python,Pandas,Dataframe,Time Series,我有一个类似这样的系列(ts),其中我将空值替换为1: Date 2013-04-28 1 2013-04-29 1 2013-04-30 1 2013-05-01 1 ... 2018-03-11 6296370000 2018-03-12 6457400000 2018-03-13

我有一个类似这样的系列(ts),其中我将空值替换为1:

       Date
    2013-04-28             1
    2013-04-29             1
    2013-04-30             1
    2013-05-01             1
           ...    
    2018-03-11    6296370000
    2018-03-12    6457400000
    2018-03-13    5991140000

Name: Volume, Length: 1808, dtype: object
ts.dtype
给了我:
dtype('O')

如何检查序列的值是否为数字,如果是字符串,如何将其转换为数字/int


提前thx

使用
应用

Ex:

import pandas as pd
df = pd.DataFrame({"a": ["1","1","1","1", "a", "a"]})
print(df["a"].apply(lambda x: int(x) if x.isdigit() else x))
0    1
1    1
2    1
3    1
4    a
5    a
Name: a, dtype: object
输出:

import pandas as pd
df = pd.DataFrame({"a": ["1","1","1","1", "a", "a"]})
print(df["a"].apply(lambda x: int(x) if x.isdigit() else x))
0    1
1    1
2    1
3    1
4    a
5    a
Name: a, dtype: object

使用
来\u数字

pd.to_numeric(df.a,errors='coerce').fillna(df.a)
Out[204]: 
   a
0  1
1  1
2  1
3  1
4  a
5  a
更多信息

pd.to_numeric(df.a,errors='coerce').fillna(df.a).apply(type)
Out[217]: 
0    <class 'float'>
1    <class 'float'>
2    <class 'float'>
3    <class 'float'>
4      <class 'str'>
5      <class 'str'>
Name: a, dtype: object
pd.to_numeric(df.a,errors='concurve').fillna(df.a).apply(type)
出[217]:
0
1.
2.
3.
4.
5.
名称:a,数据类型:对象
您可以使用
isdigit()