0

I'm trying to use the following function to display data in two CSV files and be able to merge the files later but when I run it I only get the CSV headers with no actual content


import pandas as pd


# to store the data to text file 1
def RecordData1():
    file1 = "dataSensor1.txt"
    response = ""
    outfile = open(file1, "w")
    outfile.write("Sensor ID,City,PressureReading")

    while response != '0':
        SensorID, City, PressureReading = input("Enter sensor ID, city and its pressure reading:").split()
        outfile.write("\n")
        outfile.write(SensorID + ",")
        outfile.write(City + ",")
        outfile.write(PressureReading + ",")
        response = input("Press enter to continue and zero(0) to exit:")
    outfile.close()


# to store the data into text file 2
def RecordData2() :
    file2 = "dataSensor2.txt"
    response = ""
    outfile1 = open(file2, "w")
    outfile1.write("Sensor ID,City,PressureReading")
    while response != '0':
        SensorID, City, PressureReading = input("Enter sensor ID, city and its pressure reading:").split()
        outfile1.write("\n")
        outfile1.write(SensorID + ",")
        outfile1.write(City + ",")
        outfile1.write(PressureReading + ",")
        response = input("Press enter to continue and zero(0) to exit:")
    outfile1.close()


# to read and then combine the data from 2 text files by using pandas
def RetrieveRecord():
    df1 = pd.read_csv('dataSensor1.txt', sep=",")
    df2 = pd.read_csv('dataSensor2.txt', sep=",")
    print(df1.to_string())
    print(df2.to_string())
    concatrecord(df1, df2)  # function call


def concatrecord(df1, df2):
    df3 = pd.concat([df1, df2], ignore_index=True, axis=0)
    print(df3)
    print(df3['Pressure reading'].max())
    print(df3['Pressure reading'].min())
    print('Average pressure reading',(max(df3)+min(df3)/2))
    Filteritem(df3)


def Filteritem(df3):
    choice = input('Please enter Sensor ID to be searched')
    filterItem = df3[df3['Sensor ID'] == choice]
    print(filterItem)


RecordData1()
RecordData2()
RetrieveRecord()

It's supposed to have two tables in two separate text files and be able to merge them later on into one file

Still not very sure what's the issue

Help would be highly appreciated,

Thank you

2 Answers 2

1

You are defining the column as "PressureReading" in the first two dataframes and then trying to access it in the merged dataframe by the name of "Pressure reading". Just replace this for the right name:

def concatrecord(df1, df2):
    df3 = pd.concat([df1, df2], ignore_index=True, axis=0)
    print(df3)
    print(df3['PressureReading'].max())
    print(df3['PressureReading'].min())

Then, for the average pressure reading, you can just use df["PressureReading"].mean() method:

print('Average pressure reading',df3["PressureReading"].mean())
Sign up to request clarification or add additional context in comments.

3 Comments

Still doesn't work now it gives out an error and that's it "Not enough values to unpack"
Could you post the complete error message?
I rewrote the code will post the new one as an answer
0

Rewrote the code now it works

import pandas as pd


# to store the data to text file 1
def RecordData1():
    print('Enter data for text file 1')
    file1 = "dataSensor1.txt"
    response = ""
    outfile1 = open(file1, "w")
    outfile1.write("SensorID,City,PressureReading")

    while response != '0':
        SensorID = input("Enter sensor ID:")
        City = input("Enter city:")
        PressureReading = input("Enter the pressure reading:")
        outfile1.write("\n")
        outfile1.write(SensorID + ",")
        outfile1.write(City + ",")
        outfile1.write(PressureReading)
        response = input("Press enter to continue and zero(0) to exit:")
    outfile1.close()


# to store the data into text file 2

def RecordData2() :
    print('Enter data for text file 2')
    file2 = "dataSensor2.txt"
    response = ""
    outfile2 = open(file2, "w")
    outfile2.write("SensorID,City,PressureReading")
    while response != '0':
        SensorID = input("Enter sensor ID:")
        City = input("Enter city:")
        PressureReading = input("Enter the pressure reading:")
        outfile2.write("\n")
        outfile2.write(SensorID + ",")
        outfile2.write(City + ",")
        outfile2.write(PressureReading)
        response = input("Press enter to continue and zero(0) to exit:")
    outfile2.close()


# to read and then combine the data from 2 text files by using pandas
def RetrieveRecord():
    df1 = pd.read_csv('dataSensor1.txt', sep=",")
    df2 = pd.read_csv('dataSensor2.txt', sep=",")
    print(df1.to_string())
    print(df2.to_string())


def concatrecord():
    df1 = pd.read_csv('dataSensor1.txt', sep=",")
    df2 = pd.read_csv('dataSensor2.txt', sep=",")
    df3 = pd.concat([df1, df2], ignore_index=True, axis=0)
    print(df3)

def HighestPressure():
    df1 = pd.read_csv('dataSensor1.txt', sep=",")
    df2 = pd.read_csv('dataSensor2.txt', sep=",")
    df3 = pd.concat([df1, df2], ignore_index=True, axis=0)
    new_df3 = df3.sort_values(by=["PressureReading"])
    highest = new_df3.iloc[-1, :]
    print(highest)

def LowestPressure():
    df1 = pd.read_csv('dataSensor1.txt', sep=",")
    df2 = pd.read_csv('dataSensor2.txt', sep=",")
    df3 = pd.concat([df1, df2], ignore_index=True, axis=0)
    new_df3 = df3.sort_values(by=["PressureReading"])
    lowest = new_df3.iloc[0, :]
    print(lowest)

def AvgPressure():
    df1 = pd.read_csv('dataSensor1.txt', sep=",")
    df2 = pd.read_csv('dataSensor2.txt', sep=",")
    df3 = pd.concat([df1, df2], ignore_index=True, axis=0)
    mean= df3["PressureReading"].mean()
    print('Average pressure reading:' +str(mean) +' psi')

response = 's'
while response.upper() != 'Q':
     print("-----------Gas Corporation-------------")
     print("A:Record Data 1")
     print("B:Record Data 2")
     print("C:View Data 1 and Data 2")
     print("D:View Highest Pressure Reading Details")
     print("E:View Lowest Pressure Reading Details")
     print("F:View Average Pressure Reading Result")
     print("----------------------------------------")
     choice = input("Enter choice: ")
     if choice.upper() == 'A':
        RecordData1()
        response=input("Would you like to continue?'Q' to quit:")
     elif choice.upper()== 'B':
        RecordData2()
        response = input("Would you like to continue?'Q' to quit:")
     elif choice.upper() == 'C':
        RetrieveRecord()
        concatrecord()
        response = input("Would you like to continue?'Q' to quit:")
     elif choice.upper()=='D':
         x=HighestPressure()
         print(x)
         response = input("Would you like to continue?'Q' to quit:")
     elif choice.upper()=='E':
         y=LowestPressure()
         print(y)
         response = input("Would you like to continue?'Q' to quit:")
     elif choice.upper()=='F':
         AvgPressure()
         response = input("Would you like to continue?'Q' to quit:")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.