简体   繁体   English

在Python中从CSV删除列

[英]Deleting Columns from a CSV in Python

I know similar questions to this have been asked, but I couldn't find any that were dealing with the error I'm getting (though I apologize if I'm missing something!). 我知道有人问过类似的问题,但是找不到与我遇到的错误有关的任何问题(尽管我很抱歉,如果我错过了什么!)。 I am trying to remove a few columns from a CSV that wouldn't load in Excel so I couldn't just delete them within the file. 我试图从CSV中删除一些不会在Excel中加载的列,因此我不能只在文件中删除它们。 I have the following code: 我有以下代码:

import os
import pandas as pd
os.chdir(r"C:\Users\maria\Desktop\Project\North American Breeding Bird Survey")
data = pd.read_csv("NABBSStateData.csv")
data.drop(["CountryNum", "Route", "RPID"], axis = 1, inplace = True)

but when I run it I get this error message: 但是当我运行它时,出现以下错误消息:

c:\program files (x86)\microsoft visual studio\2019\professional\common7\ide\extensions\microsoft\python\core\Packages\ptvsd\_vendored\pydevd\pydevd.py:1664: DtypeWarning: Columns (0,1,2,3,4,5,6,7,8,9,10,11,12,13) have mixed types. Specify dtype option on import or set low_memory=False.
  return self._exec(is_module, entry_point_fn, module_name, file, globals, locals)

I am relatively new to python/visual studio, and I am having a hard time figuring out what this error message is saying and how to fix it. 我是python / visual studio的新手,我很难弄清楚此错误消息在说什么以及如何解决。 Thank you!! 谢谢!!

Edit: The CSV in question is the state files from this site concatenated together, so you can open one of the state files to see the columns/data types. 编辑:有问题的CSV是来自站点的状态文件串联在一起,因此您可以打开一个状态文件以查看列/数据类型。

Looks like you have mixed data types in some of your columns (eg columns 0,1,2,3,4,5,6,7,8,9,10,11,12,13). 看起来您的某些列中混合了数据类型(例如,列0、1、2、3、4、5、6、7、8、9、10、11、12、13)。 Mixed data type means in one column, say column 'a', most rows are numbers, but there might be strings in some rows as well. 混合数据类型意味着在一列(例如列“ a”)中,大多数行是数字,但某些行中也可能包含字符串。

Try use dtype option from pd.read_csv to specify the column types. 尝试使用dtype选项从pd.read_csv指定列类型。 If you are not sure about the type, use object or str . 如果不确定类型,请使用objectstr This is an example: 这是一个例子:

df = pd.read_csv('D:\\foo.csv', header=0, dtype={'currency':str, 'v1':object, 'v2':object})

A link to use read_csv 使用read_csv的链接

Here's list of all the types you can specify . 这是您可以指定的所有类型的列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM