![]() |
python convert multiple files to multiple lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: python convert multiple files to multiple lists (/thread-39753.html) |
python convert multiple files to multiple lists - MCL169 - Apr-08-2023 year = '1990' filename = open('FF_abbreviated.txt') for i in filename: print(i) I'm trying to create a list for each line using delimiter so i can sum totals of a column
RE: python convert multiple files to multiple lists - Larz60+ - Apr-09-2023 the following will convert file contents to list year = '1990' filelist = [] with open('FF_abbreviated.txt') as fp: for line in fp: filelist.append(line.strip()) # check list contents for item in filelist: print(item) RE: python convert multiple files to multiple lists - snippsat - Apr-09-2023 (Apr-08-2023, 10:39 PM)MCL169 Wrote: I'm trying to create a list for each line using delimiter so i can sum totals of a columnThis task can also be simpler if use Pandas,as eg sum a column is a very common task do in Pandas. To give a example. import pandas as pd with open('FF_abbreviated.txt', 'r') as f: data = [i.strip().split() for i in f] columns = ['Date', 'col1', 'col2', 'col3', 'col4'] df = pd.DataFrame(data, columns=columns) df['Date'] = pd.to_datetime(df['Date'], format='%Y%m%d') df.iloc[:, 1:] = df.iloc[:, 1:].apply(pd.to_numeric)Look at data Types and the DataFrame. >>> df.dtypes Date datetime64[ns] col1 float64 col2 float64 col3 float64 col4 float64 dtype: object >>> df Date col1 col2 col3 col4 0 1926-07-01 0.09 0.22 0.30 0.009 1 1926-07-02 0.44 0.35 0.08 0.009 2 1926-07-06 0.17 0.26 0.37 0.009 3 1926-08-02 0.82 0.21 0.01 0.010 4 1926-08-03 0.46 0.39 0.38 0.010 5 1926-08-04 0.35 0.15 0.32 0.010 6 1926-09-01 0.54 0.41 0.08 0.010As you see have i convert types Data(datetime64)(which could be just integers) and float64 for rest of columns.Now can sum up. # One column >>> df['col1'].sum() 2.87 # col1 + col4 >>> df[['col1', 'col4']].values.sum() 2.936999999999999 # All columns >>> df.iloc[:, 1:].sum(axis=0) col1 2.870 col2 1.990 col3 1.540 col4 0.067 dtype: float64 RE: python convert multiple files to multiple lists - MCL169 - Apr-09-2023 (Apr-09-2023, 08:43 AM)snippsat Wrote:This is all really useful and I appreciate the input.I can't wait to get off work and go work on these things because I really want to like my professor says "own this" . Hopefully one day I can help others along the way.(Apr-08-2023, 10:39 PM)MCL169 Wrote: I'm trying to create a list for each line using delimiter so i can sum totals of a columnThis task can also be simpler if use Pandas,as eg sum a column is a very common task do in Pandas. RE: python convert multiple files to multiple lists - snippsat - Apr-09-2023 If this is basic Python training should also try to the task with Python,Pandas is a different beast🦄 To give a example,eg result of col1 2.87. lst = [] with open('FF_abbreviated.txt') as fp: for line in fp: line = line.strip().split() col1 = line[1] lst.append(float(col1))So now have list of col1 and can eg use sum() .>>> lst [0.09, 0.44, 0.17, 0.82, 0.46, 0.35, 0.54] >>> sum(lst) 2.87Can also try to do it without using sum() ,all this is good training for learning Python.
RE: python convert multiple files to multiple lists - MCL169 - Apr-09-2023 (Apr-09-2023, 06:19 PM)snippsat Wrote: If this is basic Python training should also try to the task with Python,Pandas is a different beast🦄Absolutely. Im late to the party so im looking to immerse myself in this language. I'm in a certificate course and I'm thinking about asking some of my fellow students if they want to start a study group. I follow some good Udemy courses but I really like to interactive Zoom approach maybe I'll find something that fits the bill. RE: python convert multiple files to multiple lists - Iqratech - Nov-25-2023 Sure, if you want to calculate the sum of values in col1 without using the sum() function, you can achieve this by using a loop to iterate through the values in lst and accumulate the sum. Here's an example: python Copy code lst = [0.09, 0.44, 0.17, 0.82, 0.46, 0.35, 0.54] # Calculate the sum without using sum() total_sum = 0 for value in lst: total_sum += value print("Sum of col1:", total_sum) This code initializes total_sum to 0 and then iterates through each value in lst, adding it to total_sum. Finally, it prints the calculated sum. This approach provides an alternative way to calculate the sum without relying on the built-in sum() function. |