Python 读取数据帧时数据类型不正确
我使用pandas dataframe读取如下所示的日志文件Python 读取数据帧时数据类型不正确,python,pandas,csv,dataframe,Python,Pandas,Csv,Dataframe,我使用pandas dataframe读取如下所示的日志文件 col1 col2 col3 2 3 string1 3 4 string2 5 6 string3 df = pd.read_csv(path-to-log, header=None) df.dtypes 0 object 1 object 2 object 我预计产出会有所增加 0 float64 1 float64 2 object 日志文件包含标题,因此不需要提供header=None。此外,分隔符是空白,因此可以使用
col1 col2 col3
2 3 string1
3 4 string2
5 6 string3
df = pd.read_csv(path-to-log, header=None)
df.dtypes
0 object
1 object
2 object
我预计产出会有所增加
0 float64
1 float64
2 object
日志文件包含标题,因此不需要提供
header=None
。此外,分隔符是空白,因此可以使用delim\u whitespace=True
注意,前两列作为整数而不是浮点数读入,因为只有整数存在。下面是一个演示:
import pandas as pd
from io import StringIO
mystr = StringIO("""col1 col2 col3
2 3 string1
3 4 string2
5 6 string3""")
df = pd.read_csv(mystr, delim_whitespace=True)
print(df)
col1 col2 col3
0 2 3 string1
1 3 4 string2
2 5 6 string3
print(df.dtypes)
col1 int64
col2 int64
col3 object
dtype: object
不使用页眉=无
df=pd.read\u csv('test.log',sep='')
输出-
col1 int64
col2 int64
col3 object
我注意到你的日志是空格分隔的。您是否尝试添加
sep=''
参数?同样在这种情况下,您不应该使用header=None
,因为您已经有了该行