Last active
March 21, 2022 21:50
-
-
Save haseeb-heaven/1932a77318e0b7cb736b7bba0d5d79b9 to your computer and use it in GitHub Desktop.
Read and iterate over CSV using Pandas in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Info: Read Csv file from Pandas with header. | |
#Author: Haseeb Mir. | |
#Date: 03/22/2022 | |
#%% | |
import string | |
import pandas as pd | |
def read_csv_list(csv_file:string): | |
df = pd.read_csv(csv_file,index_col=0) # Read Body data-frame. | |
df_head = pd.read_csv(csv_file,index_col=0, nrows=0).columns.tolist() # Read Headers. | |
df = df.reset_index() # make sure indexes pair with number of rows | |
data_list = [] | |
for _,row in df.iterrows():#Row with discarded index. | |
data = [] | |
for i in range(0,len(df_head)): #Iterate over headers. | |
data.append(row[df_head[i]]) | |
data_list.append(data) # Append data with headers index. | |
return data_list | |
#%% | |
##Main Method. | |
if __name__ == '__main__': | |
data_list = read_csv_list('objects.csv') | |
for data in data_list: | |
print(data,end='\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment