Python Panda错误类型错误:不支持的操作数类型为:'str'和'int'

问题描述:

我想了解如何在Python中使用Pandas。我正在对我的熊猫数据框做数学问题。 现在我的数据框看起来是这样的:Python Panda错误类型错误:不支持的操作数类型为:'str'和'int'

打印(标记)

   0  1  2  3  4   5    6 
0  447366345 -2.04 -2.69 176.98 418.84 34.3167521 -118.4068498 
1  447406197 -2.34 -2.18 176.88 418.77 34.3167522 -118.4068499 
2  447446155 -2.63 -1.56 176.74 418.77 34.3167522 -118.4068499 
3  447486653 -2.89 -0.95 176.58 418.84 34.3167522 -118.4068499 
4  447526241 -3.12 -0.42 176.43 418.84 34.3167522 -118.4068499 
5  447566373 -3.34 -0.07 176.32 418.84 34.3167522 -118.4068497 
6  447606036 -3.56 0.05 176.26 418.66 34.3167523 -118.4068497 
7  447645783 -3.77 -0.03 176.28 418.66 34.3167523 -118.4068497 
8  447686269 -3.95 -0.31 176.43 418.95 34.3167523 -118.4068497 

def data_reader(filename, rowname): 
    with open(filename, newline='') as fp: 
     yield from (row[1:] for row in csv.reader(fp, skipinitialspace=True) 
      if row[0] == rowname) 

mike = pd.DataFrame.from_records(data_reader('data.csv', 'mike')) 

现在,让我们说,我想借此行0和1000

mark_time = mark[0]/1000 

这会产生错误把它

TypeError: unsupported operand type(s) for /: 'str' and 'int' 

我猜测,因为目前我的数据帧不被视为一个IN T,所以我继续这样做:

mark_time = float (mark[0]/1000) 

但是,这也给了我同样的错误。有人可以向我解释为什么?

我的第二个问题是关于绘图。我已经很好地学习了matplotlib,并且我想在我的Panda数据框中使用它。目前我的做法是这样的:

fig1 = plt.figure(figsize= (10,10)) 
ax = fig1.add_subplot(311) 
ax.plot(mike_time, mike[0], label='mike speed', color = 'red') 
plt.legend(loc='best',prop={'size':10}) 

我可以用我的数据帧替换mike_time和mike [0]吗?

+5

类型'mark [0] = mark [0] .astype(int)' – EdChum

+5

'float(mark [0])/ 1000'而不是'float(mark [0]/1000)' – MMF

+0

@MMFI得到这个错误TypeError:无法将系列转换为

您需要使用pandas.read_csv而不是python的csv。

在那里,你可以使用D型参数为它提供正确的数据类型为它的使用方法:

从大熊猫documentation

dtype : Type name or dict of column -> type, default None Data type for data or columns. E.g. {'a': np.float64, 'b': np.int32} (unsupported with engine='python'). Use str or object to preserve and not interpret dtype.

如果必须解析CSV外面大熊猫与进口“from_records”你可以使用coerce_float = True。 Reference

coerce_float : boolean, default False Attempt to convert values to non-string, non-numeric objects (like decimal.Decimal) to floating point, useful for SQL result sets

+0

我试过这个,但是我一直得到一个错误:TypeError:'dtype'是这个函数的一个无效的关键字参数。我这样做的方式:csv.reader(fp,skipinitialspace = True,dtype = float) –

+1

这个答案是错误的,'pd.read_csv'会尝试通过嗅探它们来推断dtypes,OP使用'csv '这是完全不同的模块 – EdChum

+0

这是真的@EdChum我更新了答案 – PabTorre

您需要使用pandas read_csv,它会自动为每列分配最适当的类型。如果你有任何混合类型的列,它会警告你。然后您可以再次运行它,以明确设置类型。

+0

我试过这个,但我不断收到错误:TypeError:'dtype'是这个函数的一个无效的关键字参数。我这样做的方式:csv.reader(fp,skipinitialspace = True,dtype = float –

+1

您正在使用csv.reader,它是一个python函数,您需要使用pandas.read_csv – simon

+1

而且您不需要函数,只需df = pd.read_csv(“data.csv”) – simon