I'm just learning Python and for my first project I am trying to re-format a excel table that I can use on GIS. The Table have many columns with x for each corresponding records. I need to assign (replace the x) with the column names and concatenate all rows separated by commas. I was told that Pandas is a very good library to accomplish this. I did started (see sample code) but I am not sure what to do next. Any help or suggestions will be greatly appreciated. Here is a visual representation of what I am trying to accomplish:
Sample Code:
import pandas as pd
input_excel = r"C:\Projects\... Habitat_table.xlsx" # excel sheet path
excel = pd.read_excel(input_excel, sheet_name = 'Species_habitat') # sheet name
final_dataframe = pd.DataFrame (excel, columns=[‘Habitat_A, ‘Habitat B,C,&D’, ‘Habitat_E']) # every single column name
habitats = [‘Habitat_A, ‘Habitat B,C,&D’, ‘Habitat_E']
for index, row in final_dataframe.iterrows():
final_string = " "
print (final_dataframe.columns.name)
for h in habitats:
print(h)
for c in index:
if h in index.name: #checks if habitat is in column name
print(h)
if row[c] is not null:
final_string == final_string + c.name + ", "
print(final_string)