Error sorting data from multiple Excel files with Pandas.

Asked 2 years ago, Updated 2 years ago, 46 views

Hello! I'm an engineering student who's just learning Python. I tried to hang on while googling in my own way, but I can't even die. I think it's because I don't have the basics yet. I'd really appreciate it if you could help me!

Let me explain the question!

There are multiple Excel files and one large matrix for each file. The coding below is to sort the matrix of each file by the criteria I want Code to save only one column.

There is no problem with this code now, but only one file is sorted.

What I'm trying to do is

It automatically reads several Excel files one by one Arrange, leave one column of your choice Including all the thermal vectors, You want to create a matrix with as many columns as the number of Excel files.

I was going to post a code that I mixed "For Moon" with, but it was too busy Only the code that runs is raised like this. How should I use the for statement to make an error?

Thank you for reading this rambling article!

import numpy as np
import pandas as pd
import math
import os

path = "./"
file_list = os.listdir(path)
file_list_py = [file for file in file_list if file.endswith(".xlsx")]

data = pd.read_excel(file_list_py[0])

df20 = pd.DataFrame(data)
df20.sort_values(by=['X Location (mm)'], inplace = True)

atan2 = np.arctan2(df20['Y Location (mm)'], df20['Z Location (mm)']) * 180 / np.pi
#arctan2(x, y) is the numerator and y is the denominator
atan2 = pd.DataFrame(atan2, columns = ['atan2'])
df20 = pd.concat([df20, atan2], axis = 1)

df20yz = df20.loc[:,'Y Location (mm)':'Z Location (mm)']
dist_yz = [math.sqrt((list(df20yz.loc[k,:])[0] ** 2) + (list(df20yz.loc[k,:])[1] ** 2)) for k in range(len(df20yz))]
dist_yz = pd.DataFrame(dist_yz, columns = ['dist_yz'])
df20 = pd.concat([df20, dist_yz], axis = 1)

df20.sort_values(by=['X Location (mm)','dist_yz','atan2'], inplace = True)
df20 = df20.loc[:,'Equivalent (von-Mises) Stress (MPa)']

df20.to_excel('result.xlsx', sheet_name = 'New')

python pandas numpy

2022-09-21 15:43

1 Answers

I can't try it because I don't have an Excel file anyway.It's a processing logic. It's going to work.

Please refer to the data frame concatenation sample below.

score1
49
68
89
2
77
2
67
95
62
30
score2
23
87
9
26
85
93
46
77
4
52
score3
15
45
59
26
47
1
87
45
75
44
import glob
import pandas as pd

file_paths = [path for path in glob.glob('*.csv')]
data_frames = [pd.read_csv(path) for path in file_paths]
pd.concat(data_frames, axis=1)

    score2  score1  score3
0   23  49  15
1   87  68  45
2   9   89  59
3   26  2   26
4   85  77  47
5   93  2   1
6   46  67  87
7   77  95  45
8   4   62  75
9   52  30  44


2022-09-21 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.