Pandas Read CSV and animate the data: Difference between revisions

From wikiluntti
(Created page with "== Introduction == Read CSV data, filter, interpolate and smooth it, and the animate the plotting == Read and Interpolate == == Animate the data == == References ==")
 
Line 4: Line 4:


== Read and Interpolate ==
== Read and Interpolate ==
<syntaxhighlight lang="python">
import matplotlib.pyplot as plt
import pandas  as pd
import numpy as np
#Read, transpose, sort, drop and set the index to be the number of tetrahedrons
#and drop the columns with zero values and some parameters
df = pd.read_csv("convergence_dataOnly.csv" ).T
df.columns = ( df.iloc[0] + " " + df.iloc[1] )
df = df[3:].sort_values("Tetrahedrons n")
df.set_index("Tetrahedrons n", inplace=True)
df = df.drop('Displacement magnitude max', axis=1)
df = df.drop('von Mises Stress max', axis=1)
df = df.drop('Max Shear Stress max', axis=1)
df = df.drop('Nodes n', axis=1)
df = df.drop('Triangles n', axis=1)
#Convert values and index to numeric
df= df.apply(pd.to_numeric)
df.index = pd.to_numeric( df.index, errors='coerce' )
# Interpolate # Need to drop the one with the same number of tetrahedrons
mask = np.ones( len(df), dtype=bool)
mask[2] = False
df = df[mask]
#Get the percentage
df = df.divide( df.iloc[9] )
print(df)
df_i = df.reindex(df.index.union(np.linspace(df.index.min(),df.index.max(), df.index.shape[0]*10)))  # insert 10 NaN points between existing ones
df_i = df_i.interpolate('pchip', order=2)  # fill the gaps with values
df_i.plot()  # draw new Dataframe
</syntaxhighlight >


== Animate the data ==
== Animate the data ==


== References ==
== References ==

Revision as of 14:10, 6 July 2025

Introduction

Read CSV data, filter, interpolate and smooth it, and the animate the plotting

Read and Interpolate

import matplotlib.pyplot as plt
import pandas  as pd
import numpy as np

#Read, transpose, sort, drop and set the index to be the number of tetrahedrons
#and drop the columns with zero values and some parameters
df = pd.read_csv("convergence_dataOnly.csv" ).T
df.columns = ( df.iloc[0] + " " + df.iloc[1] )
df = df[3:].sort_values("Tetrahedrons n")
df.set_index("Tetrahedrons n", inplace=True)
df = df.drop('Displacement magnitude max', axis=1)
df = df.drop('von Mises Stress max', axis=1)
df = df.drop('Max Shear Stress max', axis=1)
df = df.drop('Nodes n', axis=1)
df = df.drop('Triangles n', axis=1)

#Convert values and index to numeric
df= df.apply(pd.to_numeric)
df.index = pd.to_numeric( df.index, errors='coerce' )

# Interpolate # Need to drop the one with the same number of tetrahedrons
mask = np.ones( len(df), dtype=bool)
mask[2] = False
df = df[mask]

#Get the percentage
df = df.divide( df.iloc[9] )
print(df)

df_i = df.reindex(df.index.union(np.linspace(df.index.min(),df.index.max(), df.index.shape[0]*10)))  # insert 10 NaN points between existing ones
df_i = df_i.interpolate('pchip', order=2)  # fill the gaps with values
df_i.plot()  # draw new Dataframe

Animate the data

References