I have a txt file that has rows and columns of data in it. I am trying to figure out how to count the number of columns (attributes) in the whole txt file. Here is my code to read the txt file and to count the columns but it is giving me the wrong answer.
import pandas as pd
data_file = pd.read_csv('3human_evolution.txt')
data_file.columns = data_file.columns.str.strip()
A=len(data_file.columns)
print(A)
data_file.columns = data_file.columns.str.strip()- doesn't make much sense to me. If you havesep=','(default), then why would you want to split columns by spaces/tabs? Can you provide a few first lines of your file?strip()will only take out spaces from left and right most given there are any. on command line if you dodf.columnsit gives a list[str]. you should do no more thanlen(df.columns)to get the count here. what's the wrong answer you getting? what did you expect ?read_csvthen you can get the number of rows and columns like this:print data_file.shape