Search code examples
pythonpandas

Trying to get input from dataframe and using values cell by cell to output into a text file


I have an excel sheet with data in following format

Bus num Bus name POI bus
20000 J874 0
20001 J976 0
10000 J1000 333333

I want to divide dataframe into two dataframes for value within POI bus column, hence one dataframe will have rows 1 and 2, and other will have row 3.

After this, I am trying to get data from both the dataframes using iloc function for all the rows, and specific columns. The code is working of dataframe 1 but not working for dataframe 2, and python is throwing " raise KeyError(key) from err".

Code I am using:

from openpyxl import load_workbook
import pandas as pd

input_file = '..\Bench\Gen_addition_total.xlsx'

wb = load_workbook(input_file)
ws = wb.active
    
output_file1 = open('..\Bench\Gen_sub.inch','w')
output_file2 = open('..\Bench\Gen_tap.inch','w')

df0 = pd.read_excel(input_file)
df1 = df0[(df0['POI 2'] == 0)]
df2 = df0[(df0['POI 2'] != 0)]

def gen_sub():
    for i in range(len(df1)):
        bus_num = (df1.iloc[:,0])
        bus_name = (df1.iloc[:,1])
        n1 = bus_num[i]
        n2 = bus_name[i]
        output_file1.write(str(n1).strip("()"))+ output_file1.write('\n') + output_file1.write(str(n2).strip("()")) + output_file1.write('\n')
    output_file1.close()
    
def gen_tap():    
    for j in range(len(df2)):
        bus_num = (df2.iloc[:,0])
        bus_name = (df2.iloc[:,1])
        n4 = bus_num[j]
        n5 = POI_bus2[j]
        output_file2.write(str(n4).strip("()"))+ output_file2.write('\n') + output_file2.write(str(n5).strip("()")) + output_file2.write('\n')
    output_file2.close()
    
gen_sub()  
gen_tap()

When I run code for just gen_sub function, code is running perfectly, and I am getting following output: 20000 J874 20001 J976

But when I am trying to run code for both the functions I am getting error: "raise KeyError(key) from err"

When I run code for just gen_sub function, code is running perfectly, and I am getting following output:

20000
J874
20001
J976

But when I am trying to run code for both the functions I am getting error:

    in gen_tap
        n4 = bus_num[j]
    
      File ~\AppData\Local\Programs\Spyder\pkgs\pandas\core\series.py:1007 in __getitem__
        return self._get_value(key)
    
      File ~\AppData\Local\Programs\Spyder\pkgs\pandas\core\series.py:1116 in _get_value
        loc = self.index.get_loc(label)
    
      File ~\AppData\Local\Programs\Spyder\pkgs\pandas\core\indexes\base.py:3655 in get_loc
        raise KeyError(key) from err
    
    KeyError: 0

Solution

  • One approach is to do this:

    import pandas as pd
    
    data = {
        'Bus num': [20000, 20001, 10000],
        'Bus name': ['J874', 'J976', 'J1000'],
        'POI bus': [0, 0, 333333]
    }
    
    df = pd.DataFrame(data)
    
    df1 = df[df['POI bus'] == 0]
    df2 = df[df['POI bus'] != 0]
    
    def gen_sub(output_file1, df1):
        with open(output_file1, 'w') as file:
            for i in range(len(df1)):
                bus_num = df1.iloc[i, 0]
                bus_name = df1.iloc[i, 1]
                file.write(f"{bus_num}\n")
                file.write(f"{bus_name}\n")
    
    def gen_tap(output_file2, df2):
        with open(output_file2, 'w') as file:
            for j in range(len(df2)):
                bus_num = df2.iloc[j, 0]
                bus_name = df2.iloc[j, 1]
                poi_bus = df2.iloc[j, 2]
                file.write(f"{bus_num}\n")
                file.write(f"{bus_name}\n")
                file.write(f"{poi_bus}\n")
    
    output_file1 = 'Gen_sub.inch'
    output_file2 = 'Gen_tap.inch'
    
    gen_sub(output_file1, df1)
    gen_tap(output_file2, df2)
    

    which gives you (in the .inch files)

    Gen_sub

    20000
    J874
    20001
    J976
    

    and Gen_tap

    10000
    J1000
    333333