285 KiB
285 KiB
<html>
<head>
</head>
</html>
Exercise 1.1¶
In [1]:
import pandas as pd
# Import built-in libraries 'string' and 'random'
import random
import string
# Get upper- and lowercase letters from 'string' library
lower = string.ascii_lowercase
upper = string.ascii_uppercase
# Create a dictionary with dummy data of integers and letters
d = {'Integers': [random.randint(1, 100) for i in range(1, 100)],
'Lowercase': [random.choice(lower) for i in range(1, 100)],
'Uppercase': [random.choice(upper) for i in range(1, 100)]}
# Create dataframe from dictionary
df = pd.DataFrame(d)
# df # <- This would print all 100 rows
Exercise 1.2¶
In [2]:
df.head(3)
Out[2]:
In [3]:
df.tail(4)
Out[3]:
Exercise 1.3¶
In [4]:
dfk = df[df['Uppercase'] == 'K']
dfk.head()
Out[4]:
Exercise 1.4¶
In [5]:
dfk = dfk.reset_index(drop=True)
dfk
Out[5]:
Exercise 2.1¶
In [27]:
# Import csv file to dataframe
dfr = pd.read_csv('shear_key_forces.csv')
dfr.head()
Out[27]:
Exercise 2.2¶
In [28]:
# Filer dataframe to contain only shear keys
dfr = dfr[dfr['Shear_key'] != 'Not_a_shear_key']
dfr.head()
Out[28]:
Exercise 2.3¶
In [29]:
dfr = dfr.drop('Element_no', axis=1)
dfr.head()
Out[29]:
Exercise 2.4¶
In [30]:
# Create groupby object and perform sum operation on it
dfr_sum = dfr.groupby(['Shear_key', 'LC', 'LC-title'], as_index=False).sum()
dfr_sum.head()
Out[30]:
The list of column labels ['Shear_key', 'LC', 'LC-title']
are the columns that are to be present in the resulting dataframe. The parameter as_index
is by default True
, which would create a multiindex out of all the column labels listed. By setting as_index=False
, we reset to 0-indexing.
Exercise 2.5¶
In [35]:
# Filter for shear key 1
df_key1 = dfr_sum[dfr_sum['Shear_key'] == 'Shear_key1']
# Use built-in pandas plot
df_key1.plot(kind='bar', x='LC', y='P[kN]', rot=45, figsize=(15, 5), color='orange', alpha=0.50)
Out[35]:
Note: The same plot could be constructed by extracting arrays in columns 'LC'
and 'P[kN]'
to variables and using matplotlib
itself as plt.bar(...)
. To do that, you would have to import matplotlib.pyplot as plt
.
pandas
just provides a shortcut to get a fast graph out.
In [ ]: