简体   繁体   English

如何检查我的 pandas csv 中是否有任何额外的列?

[英]How do I check if there are any extra columns in my pandas csv?

Currently I am trying to check a csv file if there are certain columns (whether they exist) and also to find out if there are any extra columns (that should not be there) in a certain csv file.目前,我正在尝试检查 csv 文件是否有某些列(它们是否存在),并找出某个 csv 文件中是否有任何额外的列(不应该存在)。

I have put all the required columns into a list and check the list against the columns in the file.我已将所有必需的列放入一个列表中,并根据文件中的列检查该列表。

list = ['column 1', 'column 2' , 'column 3']
for column in list:
    if column not in list:
        /execute  action

However, I am unable to find out if any extra columns already exist.但是,我无法确定是否已经存在任何额外的列。 For example csv file has 4 columns, and 3 of them is checked against the list.例如 csv 文件有 4 列,其中 3 列是根据列表检查的。

How do I get the last column to be displayed or shown?如何让最后一列显示或显示?

Thanks in advance!提前致谢!

Lets say you have a setup like this, for example:假设您有这样的设置,例如:

import pandas as pd 
list = ['column 1', 'column 2' , 'column 3']

csv_file = pd.read_csv('csv_file_here')
for col in csv_file.columns: 
    if col not in list:
        print(col) # The extra column

Try the following code (pythonic approach):尝试以下代码(pythonic 方法):

import pandas as pd 
someList = ['column 1', 'column 2' , 'column 3']

df = pd.read_csv('filename.csv')

differenceCols= list(set(df.columns) - set(someList))
print(differenceCols)

for item in differenceCols:
    # perform some action

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

相关问题 如何通过python格式化我的pandas数据框,以在csv输出中获取列? - How do I format my pandas dataframe through python to get columns in the csv output? 在不使用熊猫的情况下,如何分析CSV数据并仅从CSV文件的某些列和行中提取某些值? - WITHOUT using Pandas, how do I analyze CSV data and only extract certain values from certain columns and rows of my CSV file? 如何检查 pandas dataframe 中的列之间的冲突? - How do I check for conflict between columns in a pandas dataframe? 如何根据 Python 中的字典检查多个 pandas 列? - How do I check multiple pandas columns against a dictionary in Python? 如何沿 CSV 文件中的列写入数据? - How do I write my data along the columns in a CSV file? 如何在 Python 的多列中将我的 output 写入 CSV - How do I write my output to a CSV in multiple columns in Python Pandas CSV文件,中间偶尔会有额外的列 - Pandas CSV file with occasional extra columns in the middle 熊猫:追加现有的CSV文件,多余的列 - Pandas: Append existing CSV file, extra columns Pandas:如何在不添加额外列的情况下“合并”2 行交替 NaN 的内容? - Pandas: How do I "merge" the contents of 2 rows of alternating NaNs without adding extra columns? pandas.read_csv:如何在分层索引的 CSV 中将两列解析为日期时间? - pandas.read_csv: how do I parse two columns as datetimes in a hierarchically-indexed CSV?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM