Is there a in built method from python csv module to enumerate all possible value for a specific column? -
i have csv file has many columns. requirement find possible value present specific column.
is there built in function in python helps me these values.
you can pandas.
example file many_cols.csv
:
col1,col2,col3 1,10,100 1,20,100 2,10,100 3,30,100
find unique values per column:
>>> import pandas pd >>> df = pd.read_csv('many_cols.csv') >>> df.col1.drop_duplicates().tolist() [1, 2, 3] >>> df['col2'].drop_duplicates().tolist() [10, 20, 30] >>> df['col3'].drop_duplicates().tolist() [100]
for columns:
import pandas pd df = pd.read_csv('many_cols.csv') col in df.columns: print(col, df[col].drop_duplicates().tolist())
output:
col1 [1, 2, 3] col2 [10, 20, 30] col3 [100]
Comments
Post a Comment