pandas - 从复杂的 csv 文件创建智能矩阵
问题描述
我有一个 csv 文件,其中包含我使用 pandas 导入的 500 个时间点的 x、y、z、xy、yz 和 zx 中的对称应力张量。我需要的是在每个给定时间点为每个 ID 创建一个应力张量矩阵。图片是文件的片段。真实文件为 [502 行 x 2378 列]
我有另一个需要矩阵的 ID 列表。所以我在想脚本应该遍历 ID 列表,并且对于每个 ID,在第一行中搜索相同的后缀(例如列表中的 ID 可能是 947951),然后为每个时间点创建一个 3x3 矩阵,其中包含强调。矩阵 = [[x,xy,zx],[xy,y,yz],[zx,yz,z]]。
不知何故,新创建的矩阵仍应连接到它们的 ID,因为它们之后必须根据它们的 ID 旋转。如果您知道如何使用 numpy 函数进行 3x3 矩阵的旋转,我们将不胜感激
解决方案
这就是我解决它的方法:
df_tensors = pd.read_csv('TensorExport500.csv', delimiter=",",skiprows=[0])
print(df_tensors)
# Element ID's from previous coordinateselection to look up stress tensors in LS Dyna data
ID = [947951,948046,948141,948236,948331,12]
# Empty dictinary for appending stress tensors in upcoming for loop
matrices = {}
# Empty list for appending element IDs that are not found in LS Dyna data
IDs_not_in_data = []
# Iteration through ID list for loop up in LS Dyna data
for i in ID:
# If statement to make sure that all needed stresses are present in LS Dyna data for a given element ID for construction of stress tensor
if "X-stress-"+str(i) and 'Y-stress-'+str(i) and 'Z-stress-'+str(i) and 'XY-stress-'+str(i) and 'YZ-stress-'+str(i) and'ZX-stress-'+str(i) in df_tensors:
# Pull out columns of stresses and time for the given element ID and create a temporary dataframe inside iteration used for creating the stress tensor
temporary_df = df_tensors[['Time','X-stress-'+str(i),'Y-stress-'+str(i),'Z-stress-'+str(i),'XY-stress-'+str(i),'YZ-stress-'+str(i),'ZX-stress-'+str(i)]]
# Iterates through rows in the temporary dataframe
for index, row in temporary_df.iterrows():
# Converts scientific string to float so that numpy library can be applied for tensor/matrix rotation
x = row[1]
y = row[2]
z = row[3]
xy = row[4]
yz = row[5]
zx = row[6]
# Creates the stress tensor for the given element ID for every timespot and names it "elementid,time" and adds it to matrices dictionary
matrices[str(i)+","+str(row[0])] = np.matrix([[x,xy,zx],[xy,y,yz],[zx,yz,z]])
else:
# Append element ID if the element ID / stresses for the given element ID is not found in the LS Dyna data
IDs_not_in_data.append(i)
# Function that prints stress tensor of given element at given time if it exist
def get_stresstensor(ID,Time):
key = str(ID)+","+str(Time)
matrix = matrices.get(key)
if key in matrices:
return "The stress tensor matrix for element ID: " + str(ID) + " at the time: " + str(Time) + " is: \n" + str(matrix)
else:
return "Oh no! The stress tensor of the given element ID: " + str(ID) + " at the time: " + str(Time) + " does not exist because the data could not be found in LS Dyna data."
# Function that lists the element ID's that were not found in the LS Dyna data
def lost_IDs(list):
if len(list) == 0:
return "Hurray, stresses for all element ID's were found in the LS Dyna data"
else:
return "Oh ship, the following element IDs were not found in LSDyna export file: " + str(list)
print(get_stresstensor("947951","4.9949874665e-05"))
print(lost_IDs(IDs_not_in_data))
推荐阅读
- java - 无法从 Asterisk 服务器获得 200 OK
- android - 添加缩放后android apk大小增加(React native)
- python - 根据另一个 DataFrame 中的条件对一个 DataFrame 执行计算
- wordpress - 错误:“invalid-token-response”-WordPress 5.4.2 oauth2 客户端/Nextcloud 19(OAuth2-Provider)
- php - 用户 'xxxxxx' 已超出 'max_user_connections 或连接数过多错误
- entity-framework - EF 内存数据库中的排序依据或最大值
- c# - WPF 在多个线程中处理多个按键事件
- codeigniter-4 - getVar() 从选定的选项输入 codeigniter 4
- android-inapp-purchase - Google Play 开发者 API - 无效购买 API 响应 403
- c# - 如何从 WEB API 请求和响应 XML 中删除命名空间?