loadtxt ....
While solving the self evaluation question ,I created a file data.txt having three columns separated by ";". when I use loadtxt command it gives me an error "could not convert string to an float: 'y_5\x00'
give me the solution.
Python-3.4.3 Loading-Data-From-Files 07-08 min 50-60 sec
Answers:
The problem might arise because of the meta-text in the .csv
or .txt
file that is not really written there but is copied when its content is loaded somewhere.
I think it is better to first import your text in an array or a string and then split it and save into the dataframe specifically when your data is not too large.
import csv
arrays = []
path = "C:\\Users\\Souro\\Downloads\\AXISBANK.csv"
with open(path, 'r') as f:
reader = csv.reader(f)
for row in reader:
row = str(row).replace('\\', '') #deleting backslash
arrays.append(row)
Then take a look at arrays[:10] to find where the meta data ends and delete the unwanted data (meta data) and then converting the 'arrays' array into the dataframe. for instance:
arrays = arrays[9:]
df = pd.DataFrame(arrays[1:], columns=arrays[0]) #arrays[0] is the columns names
While solving the self evaluation question ,I created a file data.txt having three columns separated by ";". when I use loadtxt command it gives me an error "could not convert string to an float: 'y_5\x00'give me the solution.