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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40725569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:28:48  来源:igfitidea点击:

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

pythonpandas

提问by Jassica Maruo

I am trying to learn how to use Pandas in Python. I am having issue doing math to my Panda dataframe. Right now my dataframe looks something like this:

我正在尝试学习如何在 Python 中使用 Pandas。我在对 Panda 数据框进行数学运算时遇到问题。现在我的数据框看起来像这样:

print (mark)

打印(标记)

                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'))

Now let say I want to take row 0 and divide it by 1000

现在假设我想将第 0 行除以 1000

mark_time = mark[0] / 1000

This produces the error

这会产生错误

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

I am guessing because current my dataframe is not considered an INT, so I went ahead and did this:

我猜是因为当前我的数据帧不被认为是 INT,所以我继续这样做:

mark_time = float (mark[0] / 1000)

However, this also gave me the same error. Could someone please explain to me why?

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

My 2nd question is when it comes to plotting. I have learned matplotlib very well and I wanted to use it on my Panda dataframe. Currently the way I do it is this:

我的第二个问题是关于绘图的问题。我已经很好地学习了 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})

Could I just replace mike_time, and mike[0] with my dataframe?

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

回答by PabTorre

You need to use pandas.read_csv instead of python's csv.

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

There you can use the dtype argument to provide it with the correct types of data for it to use:

在那里您可以使用 dtype 参数为其提供正确的数据类型以供使用:

From pandas documentation

来自Pandas文档

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.

dtype :类型名称或列的字典 -> 类型,默认无数据或列的数据类型。例如 {'a': np.float64, 'b': np.int32} (不支持 engine='python')。使用 str 或 object 来保留而不是解释 dtype。

If you must parse the CSV outside pandas an importing with "from_records" you can use coerce_float=True. Reference

如果您必须在 Pandas 之外解析 CSV 并使用“from_records”导入,您可以使用 coerce_float=True。参考

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

coerce_float : boolean, default False 尝试将值转换为非字符串、非数字对象(如decimal.Decimal)到浮点数,对SQL结果集有用

回答by simon

You need to use pandas read_csv which will automatically assign the most appropriate type to each column. If you have any mixed type columns it will warn you. You can then run it again setting the type explicitly.

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