pandas to_csv,我错在哪里

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42407451/
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 03:02:45  来源:igfitidea点击:

to_csv, Where am I wrong

pythonpandas

提问by yun

I have this code

我有这个代码

import numpy as np
import pandas as pd
import csv

odata = pd.read_csv('email.csv')
data = odata.drop('content', axis=1, inplace=True)
data.to_csv('email-out.csv', index=False, sep=',')

And I got error like that:

我得到了这样的错误:

Traceback (most recent call last):
  File "cut.py", line 7, in <module>
    data.to_csv('email-out.csv', index=False, sep=',')
AttributeError: 'NoneType' object has no attribute 'to_csv'

Where am I wrong? help me..please

我哪里错了?请帮帮我

回答by Mac

Change this line:

改变这一行:

data = odata.drop('content', axis=1, inplace=True)

to this:

对此:

data = odata.drop('content', axis=1)

The inplaceflag causes the operation to happen in-place and return None, instead of creating a new dataframe.

inplace标志导致操作就地发生并返回None,而不是创建新的数据帧。

If you really dowant the drop to happen in place, the alternative is to replace your code with something like the following:

如果你真的这样做要下降到位的情况发生,另一种方法是用类似于下面的东西来取代你的代码:

odata = pd.read_csv('email.csv')
odata.drop('content', axis=1, inplace=True)
odata.to_csv('email-out.csv', index=False, sep=',')

Please refer to the documentationfor more info.

请参阅文档以获取更多信息。