简体   繁体   中英

how to define a function that gets the number of columns in a pandas dataframe that takes the dataframe as the function's parameter

say I got multiple pandas dataframes and I want to check their number of columns and create a def function that does that, how? Already tried on my own but when I the following code it returns a type error

import pandas as pd def load_csv(filename): filename = pd.read_csv(filename) return filename def columns_count(f): f = load_csv(f) columns = f.shape[1] return columns

Code to loop through dataframes and count number of columns

def count_num_cols(df): return len(df.columns) # or df.shape[1] list_of_paths = ["C://Users//file.txt", ] for a_path in list_of_paths: df = pd.read_csv(a_path) print(count_num_cols(df.shape[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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