Reputation: 1
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)
Upvotes: 0
Views: 410
Reputation: 11
len
gives you all elements in the DataFrame (product of rows and columns). The number of rows and columns are accessible with DataFrame.shape
. shape
gives you a tuple where the first entry is the number of rows and the second the number of columns. So you can print the number of columns with:
print(data_file.shape[1])
Upvotes: 1