Save Pandas Dataframe to .csv file

Best way is to use Panda’s to_csv() built-in function df.to_csv(‘filename’, index=False) Don’t forget to pass index=False as a second parameter if you don’t want index row saved as a column (in other words use it if you don’t want duplicated index when opening the file again). By the way, you can also withhold column names … Read more

Read and write files with ‘with’ statement in Python

I always keep forgetting how exactly with statement works with opening and reading the files in Python. path = ‘./path/filename.txt’ with open(path,’r’) as file: data = file.readlines() print(data) Or if you would like to avoid getting ‘\n’ after each line when using .readlines() you can use this instead: data = file.read().splitlines() Opening files with ‘with’ … Read more

How to iterate through dictionary in Python

First you might think in order to print dictionary values it’s enough to: for i in dict: print(i) But No. This way it’ll only print the keys. In order to print values next to the dictionary keys use: for key in dict: print(key, ‘-‘, dict[key])

TypeError: ‘int’ object is not iterable

For all noobs like me. If you get this error, you most probably doing one stupid thing like I did: where y2.shape[0] is just an integer. Remember, we can iterate only through lists or other sequences. When we would like to repeat a certain action X number of times we should do the following: If … Read more

Convert to Timestamp (DateTime) pandas time column with dates sliced in-between as headers

Hey guys. Being pretty average at Pandas, yesterday I stumbled upon a formatting challenge. I download some datasheets from the web for machine learning from time to time. This time I got some weird Time & date formatting which might’ve been good for regular use with Excel but unsuitable when it comes to Neural Networks: … Read more