如何删除csv文件中的行(非列) [英] How to delete rows (NOT columns) in a csv file
问题描述
我正在尝试删除csv
文件中的特定行(不是列)
课堂项目.当我删除列时,我输入:
I am trying to delete a particular row (NOT a column) in a csv
file for a
class project. When I deleted columns I put:
r=row
r[22], r[21]
# and so on
那么,如何指定要删除的行?我正在处理人口普查数据,并且希望摆脱人口普查表中总是存在的多余标题行. 谢谢您的帮助.
So how do I specify that I want to delete rows? I am working with census data and want to get rid of that extra row of headers that are always in census tables. Thank you for any help.
推荐答案
使用熊猫,使用它处理数据和文件非常容易.
Use pandas, it's so easy to handle data and files with it.
您可以打开csv文件,然后将其转换为熊猫数据框.
You can open your csv file and convert it to a pandas dataframe through.
df = pandas.reas_csv('file.csv')
之后,您可以使用此功能.
After that you can use this function.
df.drop(df.columns[[0]], axis=1)
在此示例中,我将删除索引为0的行.
In this example I'm deleting the row with index 0.
这篇关于如何删除csv文件中的行(非列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!