Python Forum
Questions regarding usage of pandas library
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Questions regarding usage of pandas library
#1
import pandas as pd
import matplotlib.pyplot as plt
# Open the text file in read mode
for i in range(1, 801):
    file_name = f"C:/Users/pp2c20/Downloads/IP data/IP DATA PRESSURE/HELLO-{i:03d}"
    with open(file_name, 'r') as file:
    # Read all lines from the file
         lines = file.readlines()

         first_data_1 = []
         second_data_1 = []
         
         #underside of airfoil coordinate (-0.499988,-0.00023067,-0.05)
         for line in lines[6:7]:
             # Split the line into a list of numbers
             data_list = line.strip().split(",")

             # Extract the desired data from the list and convert to float
             first_data_1.append(float(data_list[5]))
             second_data_1.append((float(data_list[5]))*106870.13)

         # underside of airfoil coordinate (-0.499982,-0.000300852,-0.05)
         first_data_2 = []
         second_data_2 = []
         
         for line in lines[7:8]:
             # Split the line into a list of numbers
             data_list = line.strip().split(",")

             # Extract the desired data from the list and convert to float
             first_data_2.append(float(data_list[5]))
             second_data_2.append((float(data_list[5]))*106870.13)
             
         # underside of airfoil coordinate (-0.499974,-0.000375813,-0.05)
         first_data_3 = []
         second_data_3 = []
         
         for line in lines[8:9]:
             # Split the line into a list of numbers
             data_list = line.strip().split(",")

             # Extract the desired data from the list and convert to float
             first_data_3.append(float(data_list[5]))
             second_data_3.append((float(data_list[5]))*106870.13)    
             
         first_data_4 = []
         second_data_4 = []
         
         # underside of airfoil (0.49996,-0.00045414,-0.05)
         for line in lines[474:475]:
              # Split the line into a list of numbers
              data_list = line.strip().split(",")

              # Extract the desired data from the list and convert to float
              first_data_4.append(float(data_list[5]))
              second_data_4.append((float(data_list[5]))*106870.13) 
         # underside of the airfoil (0.499972,-0.000374807,-0.05)      
         first_data_5 = []
         second_data_5 = []
         
         for line in lines[475:476]:
              # Split the line into a list of numbers
              data_list = line.strip().split(",")

              # Extract the desired data from the list and convert to float
              first_data_5.append(float(data_list[5]))
              second_data_5.append((float(data_list[5]))*106870.13)
              
         #underisde of the airfoil(0.499984,-0.000300251,-0.05)     
         first_data_6 = []
         second_data_6 = []
         
         for line in lines[476:477]:
               # Split the line into a list of numbers
               data_list = line.strip().split(",")

               # Extract the desired data from the list and convert to float
               first_data_6.append(float(data_list[5]))
               second_data_6.append((float(data_list[5]))*106870.13) 
         #upperisde of the airfoil(-0.499764,0.000889215,-0.05)      
         first_data_7 = []
         second_data_7 = []
         
         for line in lines[493:494]:
                 # Split the line into a list of numbers
                 data_list = line.strip().split(",")

                 # Extract the desired data from the list and convert to float
                 first_data_7.append(float(data_list[5]))
                 second_data_7.append((float(data_list[5]))*106870.13)
         #upperisde of the airfoil(-0.499709,0.000981514,-0.05)        
         first_data_8 = []
         second_data_8 = []
         
         for line in lines[495:496]:
                 # Split the line into a list of numbers
                 data_list = line.strip().split(",")

                 # Extract the desired data from the list and convert to float
                 first_data_8.append(float(data_list[5]))
                 second_data_8.append((float(data_list[5]))*106870.13) 
         #upperisde of the airfoil(-0.499646,0.00107383,-0.05)   
         first_data_9 = []
         second_data_9 = []
         
         for line in lines[496:497]:
                 # Split the line into a list of numbers
                 data_list = line.strip().split(",")

                 # Extract the desired data from the list and convert to float
                 first_data_9.append(float(data_list[5]))
                 second_data_9.append((float(data_list[5]))*106870.13)
         #print(first_data_1)

         # Create data frames for each set of data
         df1 = pd.DataFrame({
             'P*': first_data_1,
             'P': second_data_1,
             
         })
         df2 = pd.DataFrame({
             'P*': first_data_2,
             'P': second_data_2,
             
         })
         df3 = pd.DataFrame({
             'P*': first_data_3,
             'P': second_data_3,
             
         })
         df4 = pd.DataFrame({
             'P*': first_data_4,
             'P': second_data_4,
             
             
         })
         df5 = pd.DataFrame({
             'P*': first_data_5,
             'P': second_data_5,
             
             
         })
         df6 = pd.DataFrame({
             'P*': first_data_6,
             'P': second_data_6,
             
             
             
         })
         df7 = pd.DataFrame({
             'P*': first_data_7,
             'P': second_data_7,
             
             
             
         })
         df8 = pd.DataFrame({
             'P*': first_data_8,
             'P': second_data_8,
             
         })
         df9 = pd.DataFrame({
             'P*': first_data_9,
             'P': second_data_9,
         
         })
         df1.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE1.xlsx', index=False)
         df2.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE2.xlsx', index=False)
         df3.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE3.xlsx', index=False)
         df4.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE4.xlsx', index=False)
         df5.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE5.xlsx', index=False)
         df6.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE6.xlsx', index=False)
         df7.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE7.xlsx', index=False)
         df8.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE8.xlsx', index=False)
         df9.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE9.xlsx', index=False)
This is the code that i have used to extract sixth data from a text file for several number of lines. This process is repeated for 800 files.

from the code above, when i used df1.to_excel('C:/Users/pp2c20/Downloads/IP data/Pressure data/PRESSURE1.xlsx', index=False), i was expecting to have 800 lines of data. but there is only one line of data. i think it is looping over itself instead of creating a new row to put in the new data. I am unsure on how to solve this issue.
deanhystad write Apr-26-2023, 05:43 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Questions regarding usage of pandas library - by Pala - Apr-26-2023, 05:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,214 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Using pandas library ramitaherwahdan1978 5 2,451 Oct-25-2020, 01:46 PM
Last Post: jefsummers
  Library (julian), lack of usage information samsonite 12 7,022 Mar-04-2019, 06:45 AM
Last Post: samsonite
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 43,453 Nov-25-2017, 06:21 AM
Last Post: heiner55
  PyInstaller, how to create library folder instead of library.zip file ? harun2525 2 4,863 May-06-2017, 11:29 AM
Last Post: harun2525

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020