FOLLWOING IS MY CODE import pandas as pd import csv from sklearn.preprocessing import LabelEncoder from sklearn.feature_selection import chi2 with open("D:\PHD\obranking\\test_chi.csv", 'w') as csvfilew1: fields = ['index', 'feature name', 'p_value'] csvwriter1 = csv.DictWriter(csvfilew1, fieldnames=fields) csvwriter1.writeheader() for i in range(1, 5502): csv_data = dict().fromkeys(fields) csv_data['index'] = i cols =[] cols.append(int(0)) cols.append(int(i))
df = pd.read_csv("D:\PHD\obranking\\demo.csv", usecols=cols) df.apply(LabelEncoder().fit_transform) X = df.drop(labels='label', axis=1) Y = df['label'] chi_scores = chi2(X, Y) if(chi_scores[1] < 0.05): f_name = str(X.columns) f_name = f_name[8:-19] csv_data['feature name'] = f_name p_val = str(chi_scores[1]) p_val = p_val[1:-1] csv_data['p_value'] = p_val print(csv_data) csvwriter1.writerow(csv_data) #print(csv_data) #print(f_name + p_val) #print(str(X.col + str(chi_scores[1])) test_chi.csv is created but it remains empty after execution of the code. although when i am printing csv_data it gets printed but not written in csv using writerow(csv_data). Also there are no field names in the csv even writeheader() seems to not work. I am confused what is wrong. Could someone help???? -- https://mail.python.org/mailman/listinfo/python-list