<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Data Science &amp; SQL Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/category/software-engineering/data-science-and-sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/category/software-engineering/data-science-and-sql/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Thu, 11 Dec 2025 07:09:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>How to deal with date and time in SQLite</title>
		<link>https://creatronix.de/how-to-deal-with-date-and-time-in-sqlite/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Mon, 14 Nov 2022 20:41:47 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=6378</guid>

					<description><![CDATA[<p>Motivation When dealing with databases you will need the ability to store certain dates and/or timestamps in your tables. Let&#8217;s find out how you can do that in an SQLite database. SQL table creation SQLite has the data type TIMESTAMP for storing date-times CREATE TABLE social_media ( social_media_id INTEGER, insertion_date TIMESTAMP, yt_subs INTEGER fb_pg INTEGER&#8230;</p>
<p>The post <a href="https://creatronix.de/how-to-deal-with-date-and-time-in-sqlite/">How to deal with date and time in SQLite</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Motivation</h2>
<p>When dealing with databases you will need the ability to store certain dates and/or timestamps in your tables.<br />
Let&#8217;s find out how you can do that in an SQLite database.</p>
<h2>SQL table creation</h2>
<p>SQLite has the data type TIMESTAMP for storing date-times</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="SQL"><code>CREATE TABLE social_media (
        social_media_id INTEGER,
        insertion_date TIMESTAMP,
        yt_subs INTEGER
        fb_pg INTEGER
        insta_pg INTEGER,
        twitter_pg INTEGER)
</code></pre>
</div>
<h2>Python code</h2>
<p>In our Python code we will have to add this parameter to the connection</p>
<pre>detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES
</pre>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>db_connection = sqlite3.connect('bmv.db', detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES)
cursor = db_connection.cursor()
cursor.execute('''SELECT * FROM social_media ORDER BY insertion_date DESC''')
rows = cursor.fetchall()
</code></pre>
</div>
<p>Now we will get datetime objects from our query.</p>
<h2>Further Reading</h2>
<p>This article is part of our <a href="https://creatronix.de/sql-tutorial/">SQL tutorial</a>.</p>
<p>The post <a href="https://creatronix.de/how-to-deal-with-date-and-time-in-sqlite/">How to deal with date and time in SQLite</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL-Tutorial</title>
		<link>https://creatronix.de/sql-tutorial/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Mon, 17 Jan 2022 13:10:11 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=4557</guid>

					<description><![CDATA[<p>I really like SQL. But sometimes I struggle to undestand some of the concepts. So I write about it. Meanwhile there are a bunch of articles, so time for a overview page: SQL-Basics: Create, Read, Update &#38; Delete SQL-Basics: Relations SQL-Functions &#8211; SQL-Basics 3 SQLite3: Python and SQL Subqueries: Update column with values from another&#8230;</p>
<p>The post <a href="https://creatronix.de/sql-tutorial/">SQL-Tutorial</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I really like SQL. But sometimes I struggle to undestand some of the concepts. So I write about it.</p>
<p>Meanwhile there are a bunch of articles, so time for a overview page:</p>
<ol>
<li style="list-style-type: none;">
<ol>
<li><a href="https://creatronix.de/sql-basics-create-read-update-delete/">SQL-Basics: Create, Read, Update &amp; Delete</a></li>
<li><a href="https://creatronix.de/sql-basics-relations/">SQL-Basics: Relations</a></li>
<li><a href="https://creatronix.de/sql-functions/">SQL-Functions &#8211; SQL-Basics 3</a></li>
<li><a href="https://creatronix.de/sqlite3-python-and-sql/">SQLite3: Python and SQL</a></li>
<li><a href="https://creatronix.de/sql-subqueries-update-column-with-values-from-another-column/">Subqueries: Update column with values from another column</a></li>
<li><a href="https://creatronix.de/sql-null-safe-equals-operator/">SQL &#8211; NULL-safe equals operator</a></li>
<li><a href="https://creatronix.de/sql-the-dark-side/">SQL &#8211; the dark side</a></li>
<li><a href="https://creatronix.de/sql-comments/">SQL comments</a></li>
<li><a href="https://creatronix.de/how-to-deal-with-date-and-time-in-sqlite/">How to deal with date and time in SQLite</a></li>
</ol>
</li>
</ol>
<p>The post <a href="https://creatronix.de/sql-tutorial/">SQL-Tutorial</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>2021 &#8211; Advent of code &#8211; Day 3</title>
		<link>https://creatronix.de/2021-advent-of-code-day-3/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Tue, 07 Dec 2021 10:18:53 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=4318</guid>

					<description><![CDATA[<p>Part 1 You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate. Each bit in the gamma rate can be determined by finding the&#8230;</p>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-3/">2021 &#8211; Advent of code &#8211; Day 3</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Part 1</p>
<pre>You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and
the epsilon rate). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.

Each bit in the gamma rate can be determined by finding the most common bit in the corresponding position of all numbers
in the diagnostic report. For example, given the following diagnostic report:

00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010


Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.

The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.

The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.

So, the gamma rate is the binary number 10110, or 22 in decimal.

The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used.
So, the epsilon rate is 01001, or 9 in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, 198.

Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the submarine? (Be sure to represent your answer in decimal, not binary.)</pre>
<h2>Reading the data</h2>
<pre>import pandas as pd

df = pd.read_csv("./aoc_day_03_test_data.txt", dtype = str, header=None)
df.columns = ["original"]</pre>
<p><span id="more-4318"></span>We need to define the input data as string via dtype = str. Otherwise it will be treated as integer and we loose the leading zeroes.</p>
<table class="table-striped" border="1">
<thead>
<tr>
<th></th>
<th>original</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>00100</td>
</tr>
<tr>
<th>1</th>
<td>11110</td>
</tr>
<tr>
<th>2</th>
<td>10110</td>
</tr>
<tr>
<th>3</th>
<td>10111</td>
</tr>
<tr>
<th>4</th>
<td>10101</td>
</tr>
<tr>
<th>5</th>
<td>01111</td>
</tr>
<tr>
<th>6</th>
<td>00111</td>
</tr>
<tr>
<th>7</th>
<td>11100</td>
</tr>
<tr>
<th>8</th>
<td>10000</td>
</tr>
<tr>
<th>9</th>
<td>11001</td>
</tr>
<tr>
<th>10</th>
<td>00010</td>
</tr>
<tr>
<th>11</th>
<td>01010</td>
</tr>
</tbody>
</table>
<p>Now we can split the strings into individual bits with the split() function.</p>
<p>To generate a new column for every bit we use the tolist() function. The first and the last column is empty, so let&#8217;s remove them.</p>
<pre>df = pd.DataFrame(df["original"].str.split('').tolist())
del df[0]
df = df.iloc[:,:-1]
df</pre>
<table class="dataframe" border="1">
<thead>
<tr>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<th>2</th>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<th>3</th>
<td>1</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>4</th>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>5</th>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>6</th>
<td>0</td>
<td>0</td>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>7</th>
<td>1</td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>8</th>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<th>9</th>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>10</th>
<td>0</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
<tr>
<th>11</th>
<td>0</td>
<td>1</td>
<td>0</td>
<td>1</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>Now we have to look which value appears the most in each column.</p>
<pre>df = pd.concat([df[column].value_counts() for column in df], axis = 1) 
df</pre>
<table class="dataframe" border="1">
<thead>
<tr>
<th></th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<td>7</td>
<td>5</td>
<td>8</td>
<td>7</td>
<td>5</td>
</tr>
<tr>
<th>0</th>
<td>5</td>
<td>7</td>
<td>4</td>
<td>5</td>
<td>7</td>
</tr>
</tbody>
</table>
<p>The most common bits are</p>
<pre>most_common_bits = df.idxmax()
most_common_bits</pre>
<pre class="console_text">1    1
2    0
3    1
4    1
5    0
dtype: object</pre>
<p>Now we have to concatenate these bits to one string and  convert it back into an integer</p>
<pre>gamma_rate = ''.join(most_common_bits)
gamma_rate = int(gamma_rate, 2)
gamma_rate</pre>
<p>That gives us the gamma rate of 22</p>
<p>The epsilon rate can be calculated by inverting all significant bits in the most_common_bits value. We do this by creating a bit mask</p>
<pre>bit_mask = 2 ** len(most_common_bits) - 1

</pre>
<p>and applying it with an XOR to the gamma rate:</p>
<pre>epsilon_rate = gamma_rate ^ bit_mask
epsilon_rate</pre>
<p>The solution is:</p>
<pre>gamma_rate * epsilon_rate</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-3/">2021 &#8211; Advent of code &#8211; Day 3</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>2021 – Advent of code – Day 2</title>
		<link>https://creatronix.de/2021-advent-of-code-day-2/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 03 Dec 2021 11:23:34 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[advent of code]]></category>
		<category><![CDATA[day 2]]></category>
		<category><![CDATA[pandas]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=4296</guid>

					<description><![CDATA[<p>Part 1 Today the puzzle got a bit trickier than Day 1. The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it's going. For example: forward 5 down 5 forward 8 up 3 down 8 forward 2 Your horizontal position and depth both start at 0.&#8230;</p>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-2/">2021 – Advent of code – Day 2</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Part 1</h2>
<p>Today the puzzle got a bit trickier than <a href="https://creatronix.de/2021-advent-of-code-day-1/">Day 1</a>.</p>
<pre>The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it's going. For example:

    forward 5
    down 5
    forward 8
    up 3
    down 8
    forward 2

Your horizontal position and depth both start at 0. The steps above would then modify them as follows:

    forward 5 adds 5 to your horizontal position, a total of 5.
    down 5 adds 5 to your depth, resulting in a value of 5.
    forward 8 adds 8 to your horizontal position, a total of 13.
    up 3 decreases your depth by 3, resulting in a value of 2.
    down 8 adds 8 to your depth, resulting in a value of 10.
    forward 2 adds 2 to your horizontal position, a total of 15.

After following these instructions, you would have a horizontal position of 15 and a depth of 10. (Multiplying these together produces 150.)

Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?</pre>
<p>So Pandas here we go again:<span id="more-4296"></span></p>
<pre>df = pd.read_csv("./aoc_day_02_data.txt", delimiter=" ",header=None)
df.columns = ["command", "value"]</pre>
<p>Alright, reading in the data and naming the columns are the same steps as yesterday. Now we have to columns.</p>
<table class="dataframe" border="1">
<thead>
<tr>
<th></th>
<th>0</th>
<th>1</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>forward</td>
<td>5</td>
</tr>
<tr>
<th>1</th>
<td>down</td>
<td>5</td>
</tr>
<tr>
<th>2</th>
<td>forward</td>
<td>8</td>
</tr>
<tr>
<th>3</th>
<td>up</td>
<td>3</td>
</tr>
<tr>
<th>4</th>
<td>down</td>
<td>8</td>
</tr>
<tr>
<th>5</th>
<td>forward</td>
<td>5</td>
</tr>
</tbody>
</table>
<pre>horizontal = df[df['command']=="forward"]["value"].sum()</pre>
<p>The horizontal value can be calculated with the sum function when we filter the data frame to rows where the command is &#8220;forward&#8221;</p>
<p>&nbsp;</p>
<pre>depth = df[df['command']=="down"]["value"].sum() - df[df['command']=="up"]["value"].sum()</pre>
<p>The depth can calculated by summing up the down and up commands separately and subtract the sums from each other.</p>
<p>Now we have to multiply the depth and the position to get the solution</p>
<pre>position = depth * horizontal</pre>
<h2>Part 2</h2>
<pre>    down X increases your aim by X units.
    up X decreases your aim by X units.
    forward X does two things:
        It increases your horizontal position by X units.
        It increases your depth by your aim multiplied by X.

Now, the above example does something different:

    forward 5 adds 5 to your horizontal position, a total of 5. Because your aim is 0, your depth does not change.
    down 5 adds 5 to your aim, resulting in a value of 5.
    forward 8 adds 8 to your horizontal position, a total of 13. Because your aim is 5, your depth increases by 8*5=40.
    up 3 decreases your aim by 3, resulting in a value of 2.
    down 8 adds 8 to your aim, resulting in a value of 10.
    forward 2 adds 2 to your horizontal position, a total of 15. Because your aim is 10, your depth increases by 2*10=20 to a total of 60.

After following these new instructions, you would have a horizontal position of 15 and a depth of 60. 
(Multiplying these produces 900.)

Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. 
What do you get if you multiply your final horizontal position by your final depth?</pre>
<p>To get an overview I simplified the table</p>
<pre>     a   d
f 5  0   0
d 5  5
f 8  5  40
u 3  2
d 8 10
f 2 10  20</pre>
<p>Here I had a hard time to do it with pandas so vanilla python to the rescue:</p>
<pre>if __name__ == '__main__':

    with open("./aoc_day_02_data.txt") as file:
        lines = file.readlines()
        lines = [line.rstrip() for line in lines]

    horizontal = 0
    current_aim = 0
    depth = 0
    for line in lines:
        print(line)
        command, value = line.split(" ")
        value = int(value)
        if command == "forward":
            horizontal += value
            depth += value * current_aim
        if command == "down":
            current_aim += value
        if command == "up":
            current_aim += value * -1

    print(f"horizontal: {horizontal}")
    print(f"depth: {depth}")
    print(horizontal * depth)</pre>
<h2>Update</h2>
<p>I&#8217;ve figured out how to do it with Pandas as well</p>
<pre>import pandas as pd

df = pd.read_csv("./aoc_day_02_test_data.txt", delimiter=" ",header=None)
df.columns = ["command", "value"]

horizontal = df[df['command']=="forward"]["value"].sum()

df.loc[df['command']=="up", "value"] = df[df['command']=="up"].mul(-1)
df["aim"] = 0

df.loc[df['command']!="forward", "aim"] = df[df['command']!="forward"]["value"]
df["current_aim"] = df["aim"].cumsum()

df.loc[df['command']=="forward", "depth"] = df[df['command']=="forward"]["value"] * df[df['command']=="forward"]["current_aim"]
depth = df["depth"].sum()</pre>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-2/">2021 – Advent of code – Day 2</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>2021 &#8211; Advent of code &#8211; Day 1</title>
		<link>https://creatronix.de/2021-advent-of-code-day-1/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Thu, 02 Dec 2021 09:30:22 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[advent of code]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[jupyter]]></category>
		<category><![CDATA[pandas]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=4285</guid>

					<description><![CDATA[<p>I&#8217;ve haven&#8217;t participated in the advent of code before. But always been curious. What is advent of code? It&#8217;s an advent Calendar for programmers. You get 25 challenges starting December 1st. Caveat: you have to solve the challenge to be eligible for the next day&#8217;s challenge 🙂 Day 1 Challenge &#8211; Part 1 On the&#8230;</p>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-1/">2021 &#8211; Advent of code &#8211; Day 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ve haven&#8217;t participated in the <a href="https://adventofcode.com/2021/day/1">advent of code</a> before. But always been curious.</p>
<h2>What is advent of code?</h2>
<p>It&#8217;s an <span class="Y2IQFc" lang="en">advent Calendar for programmers. You get 25 challenges starting December 1st. Caveat: you have to solve the challenge to be eligible for the next day&#8217;s challenge 🙂<br />
</span><span id="more-4285"></span></p>
<h2>Day 1 Challenge &#8211; Part 1</h2>
<p>On the first day your first task is to count how many times a value is bigger than its predecessor. They give us some sample data</p>
<pre>199 N/A
200 <strong>bigger</strong>
208 <strong>bigger</strong>
210 <strong>bigger</strong>
200 smaller
207 <strong>bigger</strong>
240 <strong>bigger</strong>
269 <strong>bigger</strong>
260 smaller
263 <strong>bigger</strong></pre>
<p>When we count the times a value is bigger we get seven times bigger.</p>
<p>The actual data contains 2000 rows. This isn&#8217;t exactly big data but I&#8217;ve wanted to dust off my Pandas skill, so here we go:</p>
<p>Let&#8217;s look at the data</p>
<pre>import pandas as pd

df = pd.read_csv("./aoc_day_01_data.txt", header=None)
df.describe</pre>
<p>With the read_csv() function we can read in our data file and convert it into a data frame. It&#8217;s important to hand over the header=None. Otherwise pandas assumes the first row is a column header.</p>
<p>df.describe gives us:</p>
<pre class="console_text">&lt;bound method NDFrame.describe of          0
0      159
1      158
2      174
3      196
4      197
...    ...
1995  8538
1996  8543
1997  8545
1998  8557
1999  8568

[2000 rows x 1 columns]&gt;</pre>
<p>Because we want to reference the columns by name we add a column header</p>
<pre>df.columns = ["original"]</pre>
<p>To compare the nth cell with its n+1th cell neighbour be add a new column but shift the values</p>
<pre>df['shifted'] = df['original'].shift(-1)</pre>
<p>The output looks like this:</p>
<table class="dataframe" border="1">
<thead>
<tr>
<th></th>
<th>original</th>
<th>shifted</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>159</td>
<td><strong>158</strong>.0</td>
</tr>
<tr>
<th>1</th>
<td><strong>158</strong></td>
<td>174.0</td>
</tr>
<tr>
<th>2</th>
<td>174</td>
<td>196.0</td>
</tr>
<tr>
<th>3</th>
<td>196</td>
<td>197.0</td>
</tr>
<tr>
<th>4</th>
<td>197</td>
<td>194.0</td>
</tr>
<tr>
<th>&#8230;</th>
<td>&#8230;</td>
<td>&#8230;</td>
</tr>
<tr>
<th>1995</th>
<td>8538</td>
<td>8543.0</td>
</tr>
<tr>
<th>1996</th>
<td>8543</td>
<td>8545.0</td>
</tr>
<tr>
<th>1997</th>
<td>8545</td>
<td>8557.0</td>
</tr>
<tr>
<th>1998</th>
<td>8557</td>
<td>8568.0</td>
</tr>
<tr>
<th>1999</th>
<td>8568</td>
<td>NaN</td>
</tr>
</tbody>
</table>
<p>We add another column where we place the value True when the value from the current row in the shifted column is bigger than in the original column:</p>
<pre>df['increased'] = (df['shifted'] &gt; df['original'])</pre>
<p>Now it starts to look like the sample data from the introduction:</p>
<table class="dataframe" border="1">
<thead>
<tr>
<th></th>
<th>original</th>
<th>shifted</th>
<th>increased</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>159</td>
<td>158.0</td>
<td>False</td>
</tr>
<tr>
<th>1</th>
<td>158</td>
<td>174.0</td>
<td>True</td>
</tr>
<tr>
<th>2</th>
<td>174</td>
<td>196.0</td>
<td>True</td>
</tr>
<tr>
<th>3</th>
<td>196</td>
<td>197.0</td>
<td>True</td>
</tr>
<tr>
<th>4</th>
<td>197</td>
<td>194.0</td>
<td>False</td>
</tr>
<tr>
<th>&#8230;</th>
<td>&#8230;</td>
<td>&#8230;</td>
<td>&#8230;</td>
</tr>
<tr>
<th>1995</th>
<td>8538</td>
<td>8543.0</td>
<td>True</td>
</tr>
<tr>
<th>1996</th>
<td>8543</td>
<td>8545.0</td>
<td>True</td>
</tr>
<tr>
<th>1997</th>
<td>8545</td>
<td>8557.0</td>
<td>True</td>
</tr>
<tr>
<th>1998</th>
<td>8557</td>
<td>8568.0</td>
<td>True</td>
</tr>
<tr>
<th>1999</th>
<td>8568</td>
<td>NaN</td>
<td>False</td>
</tr>
</tbody>
</table>
<p>the last thing we have to do is counting how many times True occurs:</p>
<pre>true_count = df['increased'].sum()</pre>
<p>which gives us &#8220;1583&#8221;</p>
<p>This is a bit of a hack because it assumes that True equals 1 and False == 0</p>
<p>A more elegant solution is to use value_counts:</p>
<pre>df['increased'].value_counts(dropna=False)</pre>
<p>No the output is:</p>
<pre class="console_text">True     1583
False     417
Name: increased, dtype: int64</pre>
<p>And 1583 is the number we are looking for. This earned us our first golden star and unlocked the second part of the challenge:</p>
<h2>Part 2</h2>
<p>The second part is a bit more challenging because we have to sum up three adjacent values and compare them to the next three values.</p>
<pre>199  A       
200  A B     
208  A B C   
210    B C D
200  E   C D
207  E F   D
240  E F G
269    F G H
260      G H
263        H</pre>
<p>I created a new notebook and started like part 1 with reading the data and naming the first column</p>
<pre>import pandas as pd

df = pd.read_csv("./aoc_day_01_data.txt", header=None)
df.columns = ["original"]</pre>
<p>To add the sum of three values to the row of the first value we use the following code</p>
<pre>indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=3)
df["rolling_sum"] = df.original.rolling(window=indexer).sum()</pre>
<p>This demonstrates the power of Pandas once more: you have integrated sliding window functions!</p>
<p>The rest is equal to part one &#8220;shift, compare and count&#8221;</p>
<pre>df['shifted_rs'] = df['rolling_sum'].shift(-1)
df['increased_rs'] = (df['shifted_rs'] &gt; df['rolling_sum'])
true_count = df['increased_rs'].sum()
true_count</pre>
<p>As a little Fingerübung I did the same with vanilla Python:</p>
<pre>data = []
with open("./aoc_day_01_test_data.txt") as f:
    for line in f:
        data.append(int(line.rstrip()))

triplet_sums = []

for i, v in enumerate(data):
    if i &lt; (len(data) - 2):
        triplet_sum = data[i] + data[i+1] + data[i+2]
        triplet_sums.append(triplet_sum)
print(triplet_sums)

sums_larger_than_previous_sums = 0
for i, v in enumerate(triplet_sums):
    if i &lt; (len(triplet_sums) - 1):
        if triplet_sums[i] &lt; triplet_sums[i+1]:
            sums_larger_than_previous_sums += 1

print(sums_larger_than_previous_sums)</pre>
<p>Which works but is less elegant.</p>
<p>Stay tuned for more!</p>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-1/">2021 &#8211; Advent of code &#8211; Day 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>k-fold crossvalidation with sklearn</title>
		<link>https://creatronix.de/k-fold-crossvalidation-with-sklearn/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 05 Mar 2021 13:39:36 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3126</guid>

					<description><![CDATA[<p>from sklearn.model_selection import KFold kf = KFold(n_splits=2) kf.split(df_train) step = 0 # set counter to 0 for train_index, val_index in kf.split(df_train): # for each fold step = step + 1 # update counter print('Step ', step) features_fold_train = df_train.iloc[train_index, [4, 5]] # features matrix of training data (of this step) features_fold_val = df_train.iloc[val_index, [4, 5]]&#8230;</p>
<p>The post <a href="https://creatronix.de/k-fold-crossvalidation-with-sklearn/">k-fold crossvalidation with sklearn</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>from sklearn.model_selection import KFold

kf = KFold(n_splits=2)

kf.split(df_train)

step = 0 # set counter to 0
for train_index, val_index in kf.split(df_train): # for each fold
    step = step + 1 # update counter

    print('Step ', step)

    features_fold_train = df_train.iloc[train_index, [4, 5]] # features matrix of training data (of this step)
    features_fold_val = df_train.iloc[val_index, [4, 5]] # features matrix of validation data (of this step) 

    target_fold_train = df_train.iloc[train_index, 6] # target vector of training data (of this step)
    target_fold_val = df_train.iloc[val_index, 6] # target vector of validation data (of this step) 

    print("VALIDATE:", val_index)
    print('Dimensions features matrix for validation: ', features_fold_val.shape)
    print("TRAIN:", train_index)
    print('Dimensions features matrix for training: ',features_fold_train.shape, '\n')

</code></pre>
</div>
<p>further info</p>
<p><iframe title="[S1E6] Cross-Validation | 5 Minutes With Ingo" width="1200" height="675" src="https://www.youtube.com/embed/gDYvLvzPXG0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe></p>
<p>The post <a href="https://creatronix.de/k-fold-crossvalidation-with-sklearn/">k-fold crossvalidation with sklearn</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Pandas Cheat Sheet</title>
		<link>https://creatronix.de/pandas-cheat-sheet/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 05 Mar 2021 13:17:19 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[cheat sheet]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[cumsum]]></category>
		<category><![CDATA[cumulative sum]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[delimiter]]></category>
		<category><![CDATA[dropping columns]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[head tail]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[iloc]]></category>
		<category><![CDATA[indexing]]></category>
		<category><![CDATA[loc]]></category>
		<category><![CDATA[pandas]]></category>
		<category><![CDATA[renamin columns]]></category>
		<category><![CDATA[rolling sum]]></category>
		<category><![CDATA[shifting]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[splitting strings]]></category>
		<category><![CDATA[value_counts]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3112</guid>

					<description><![CDATA[<p>If you are new to Pandas feel free to read Introduction to Pandas I&#8217;ve assembled some pandas code snippets Reading Data Reading CSV import pandas as pd # read from csv df = pd.read_csv("path_to_file") Can also be textfiles. file suffix is ignored The default limiter for comma separated value files is the comma. If you&#8230;</p>
<p>The post <a href="https://creatronix.de/pandas-cheat-sheet/">Pandas Cheat Sheet</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you are new to Pandas feel free to read <a href="https://creatronix.de/introduction-to-pandas/">Introduction to Pandas</a></p>
<p>I&#8217;ve assembled some pandas code snippets</p>
<h2>Reading Data</h2>
<h3>Reading CSV</h3>
<pre>import pandas as pd

# read from csv
df = pd.read_csv("path_to_file")</pre>
<p>Can also be textfiles. file suffix is ignored</p>
<p>The default limiter for comma separated value files is the comma. If you have data with another delimiter you can specify it via:</p>
<pre>delimiter=";"</pre>
<p>If your data has no header you can pass header=None into the function</p>
<pre>df = pd.read_csv("./aoc_day_01_data.txt", header=None)</pre>
<p>With skiprows you can start reading in at any row</p>
<pre>skiprows=8</pre>
<p>Sometimes you need to alter the encoding as well:</p>
<pre>encoding="cp1252"</pre>
<h3>Reading Excel</h3>
<p>You can read excel files as well but you need to install</p>
<pre>pip install openpyxl</pre>
<pre>df = pd.read_excel("./my_excel_sheet.xlsx")</pre>
<p>With sheet_name you can select the individual sheet:</p>
<pre>sheet_name="my_sheet_1"</pre>
<h2>Inspecting data</h2>
<h3>Basic information</h3>
<pre>df.describe()</pre>
<h3>Length</h3>
<pre>len(df)</pre>
<h3>showing entries</h3>
<pre>df.head()</pre>
<pre>df.tail(10)</pre>
<h3>Indexing</h3>
<pre><span class="n">df</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span></pre>
<p>gives you column A</p>
<p>iloc gives you entries based on numerical index</p>
<pre>#      [row, column]
df.iloc[0,   0]</pre>
<pre>#     [row, column]
df.loc[:, :]</pre>
<h2>Data Cleaning</h2>
<h3>Dropping columns</h3>
<pre>del df["column_name"]</pre>
<h3>Renaming columns</h3>
<pre>df.columns = ["new_column_name", ...]</pre>
<h3>Comparing columns</h3>
<pre>df['increased'] = (df['shifted'] &gt; df['original'])</pre>
<h3>Shifting columns</h3>
<pre>df['shifted'] = df['original'].shift(-1)</pre>
<h2>Splitting</h2>
<h3>Splitting strings into individual columns</h3>
<p>&nbsp;</p>
<pre>df = pd.DataFrame(df["original"].str.split('').tolist())</pre>
<h2></h2>
<h2>Counting and Calculating</h2>
<h3>Summing columns</h3>
<pre>df["value"].sum()</pre>
<h3>Cumulative sum</h3>
<pre>df["aim"].cumsum()</pre>
<h3>Rolling sum</h3>
<pre>indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=3)
df["rolling_sum"] = df.original.rolling(window=indexer).sum()</pre>
<h3>Counting value occurence</h3>
<p>&nbsp;</p>
<pre>df['increased'].value_counts(dropna=False)</pre>
<h3>Counting occurrences for all columns</h3>
<pre>df = pd.concat([df[column].value_counts() for column in df], axis = 1)</pre>
<h3>Convert column to datetime</h3>
<p>&nbsp;</p>
<pre>df.loc[:, 'date'] = pd.to_datetime(df.loc[:, 'date'])</pre>
<h3>Convert datetime to minutes since midnight</h3>
<p>&nbsp;</p>
<pre>df_train.loc[:, 'msm'] = df_train.loc[:, "date"].dt.hour * 60 + df_train.loc[:, "date"].dt.minute</pre>
<p>The post <a href="https://creatronix.de/pandas-cheat-sheet/">Pandas Cheat Sheet</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Data Science Pipeline</title>
		<link>https://creatronix.de/data-science-pipeline/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 05 Mar 2021 13:16:11 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3046</guid>

					<description><![CDATA[<p>Motivation Learning Data Science can be grueling and overwhelming sometimes. When I feel too overwhelmed it&#8217;s time to draw a picture. This my current overview of what a data scientist has to do: General tools Linear Algebra with numpy numpy random choice Numpy linspace function Data acquisiton Data Science Datasets: Iris flower data set Data&#8230;</p>
<p>The post <a href="https://creatronix.de/data-science-pipeline/">Data Science Pipeline</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Motivation</h2>
<p>Learning Data Science can be grueling and overwhelming sometimes.</p>
<p>When I feel too overwhelmed it&#8217;s time to draw a picture.</p>
<p>This my current overview of what a data scientist has to do:</p>
<p><img fetchpriority="high" decoding="async" class="alignnone size-large wp-image-3050" src="https://creatronix.de/wp-content/uploads/2019/12/data_science_pipeline-363x1024.png" alt="" width="363" height="1024" srcset="https://creatronix.de/wp-content/uploads/2019/12/data_science_pipeline-363x1024.png 363w, https://creatronix.de/wp-content/uploads/2019/12/data_science_pipeline-106x300.png 106w, https://creatronix.de/wp-content/uploads/2019/12/data_science_pipeline.png 600w" sizes="(max-width: 363px) 100vw, 363px" /></p>
<h2>General tools</h2>
<p><a href="https://creatronix.de/linear-algebra-with-numpy-part-1/">Linear Algebra with numpy</a></p>
<p><a href="https://creatronix.de/numpy-random-choice/">numpy random choice</a></p>
<p><a href="https://creatronix.de/numpy-linspace-function/">Numpy linspace function</a></p>
<h2>Data acquisiton</h2>
<p><a href="https://creatronix.de/data-science-datasets-iris-flower-data-set/">Data Science Datasets: Iris flower data set</a></p>
<h2>Data cleaning</h2>
<p><a href="https://creatronix.de/pandas-cheat-sheet/">Pandas Cheat Sheet</a></p>
<h2>Data exploration</h2>
<p><a href="https://creatronix.de/introduction-to-pandas/">Introduction to Pandas</a></p>
<h2>Feature Scaling</h2>
<p><a href="https://creatronix.de/feature-scaling/">Feature Scaling</a><a href="https://creatronix.de/k-fold-crossvalidation-with-sklearn/"></a></p>
<h2>Estimator fit</h2>
<p><a href="https://creatronix.de/linear-regression-with-sklearn-cheat-sheet/">Linear Regression with sklearn cheat sheet</a></p>
<p><a href="https://creatronix.de/lesson-2-naive-bayes/">Lesson 2: Naive Bayes</a></p>
<p><a href="https://creatronix.de/lesson3-support-vector-machines/">Lesson 3: Support Vector Machines</a></p>
<h3>Validation</h3>
<p><a href="https://creatronix.de/what-is-cross-validation/">What is Cross-Validation in Data Science?</a></p>
<p><a href="https://creatronix.de/k-fold-crossvalidation-with-sklearn/">k-fold crossvalidation with sklearn</a></p>
<h2>Visualization</h2>
<p><a href="https://creatronix.de/introduction-to-matplotlib/">Introduction to matplotlib</a></p>
<p><a href="https://creatronix.de/introduction-to-matplotlib-part-2/">Introduction to matplotlib &amp;#8211; Part 2</a></p>
<p><a href="https://creatronix.de/introduction-to-matplotlib-part-3/">Introduction to matplotlib – Part 3</a></p>
<p><a href="https://creatronix.de/scatterplot-with-matplotlib/">Scatterplot with matplotlib</a></p>
<h2>Interpretation</h2>
<p>Metrics about the quality of your model</p>
<p><a href="https://creatronix.de/classification-precision-and-recall/">Classification: Precision and Recall</a></p>
<p><a href="https://creatronix.de/receiver-operating-characteristic/">Receiver Operating Characteristic</a></p>
<p><a href="https://creatronix.de/confusion-matrix/">Confusion Matrix</a></p>
<p>The post <a href="https://creatronix.de/data-science-pipeline/">Data Science Pipeline</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Introduction to the Julia programming language</title>
		<link>https://creatronix.de/introduction-to-the-julia-programming-language/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Mon, 08 Jun 2020 12:21:22 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=3134</guid>

					<description><![CDATA[<p>Installation You can download the installer at https://julialang.org/ On Windows you have to set the path manually, e.g. C:\Julia-1.3.1\bin Let&#8217;s fire up a command line and type: julia You will be greeted with a nice little ascii art and the prompt. If You want to close the prompt You can use CTRL + D Hello&#8230;</p>
<p>The post <a href="https://creatronix.de/introduction-to-the-julia-programming-language/">Introduction to the Julia programming language</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Installation</h2>
<p>You can download the installer at <a href="https://julialang.org/">https://julialang.org/</a></p>
<p>On Windows you have to set the path manually, e.g. C:\Julia-1.3.1\bin</p>
<p>Let&#8217;s fire up a command line and type: julia</p>
<p><img decoding="async" class="alignnone wp-image-3137 size-full" src="https://creatronix.de/wp-content/uploads/2020/03/julia.png" alt="" width="595" height="209" srcset="https://creatronix.de/wp-content/uploads/2020/03/julia.png 595w, https://creatronix.de/wp-content/uploads/2020/03/julia-300x105.png 300w" sizes="(max-width: 595px) 100vw, 595px" /></p>
<p>You will be greeted with a nice little ascii art and the prompt.</p>
<p>If You want to close the prompt You can use CTRL + D</p>
<h2>Hello Julia</h2>
<p>To get over with, here is the hello world</p>
<pre>julia&gt; show("Hello Julia")
"Hello Julia"</pre>
<p>Julia has a show function which gives you decorated text interpretations</p>
<p>But there is also a print function</p>
<pre>julia&gt; print("Hello Julia")
Hello Julia</pre>
<p>&nbsp;</p>
<h2>Writing Programs</h2>
<p>julia code is put into files with the file extension .jl</p>
<p>so let&#8217;s write the hello julia to a file</p>
<p>We can start it via</p>
<pre>julia hello-julia.jl</pre>
<h3>Reading and Writing Files</h3>
<pre>open("data.txt") do file
    for line in eachline(file)
        println(line)
    end
end</pre>
<p>asdf</p>
<pre>open("data.txt") do file
    for line in enumerate(eachline(file))
        println(line[1], ": ", line[2])
    end
end</pre>
<h3>Reading CSV</h3>
<p>As a developer with interest in data science I want to know what packages exist to handle data. The first one is the csv package to deal with comma separated values.</p>
<pre>julia&gt; import Pkg; Pkg.add("CSV")</pre>
<pre>julia&gt;using CSV
julia&gt;csv = CSV.read("./my_cars.csv")

5×4 DataFrames.DataFrame
│ Row │ Make   │  Model  │  Bought │  Sold │
│     │ String │ String  │ Int64   │ Int64 │
├─────┼────────┼─────────┼─────────┼───────┤
│ 1   │ Fiat   │  Punto  │ 2005    │ 2009  │
│ 2   │ Ford   │  Escort │ 2009    │ 2010  │
│ 3   │ Ford   │  Fusion │ 2003    │ 2012  │
│ 4   │ Audi   │  A5     │ 2012    │ 2099  │
│ 5   │ Honda  │  Jazz   │ 2015    │ 2099  │

</pre>
<h3>Plots</h3>
<pre>import Pkg; Pkg.add("Plots")
using Plots
x = 1:10; 
y = rand(10); 
plot(x, y)
</pre>
<p>The post <a href="https://creatronix.de/introduction-to-the-julia-programming-language/">Introduction to the Julia programming language</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQLite3: Python and SQL</title>
		<link>https://creatronix.de/sqlite3-python-and-sql/</link>
					<comments>https://creatronix.de/sqlite3-python-and-sql/#respond</comments>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Thu, 02 Apr 2020 08:57:45 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<guid isPermaLink="false">http://creatronix.de/?p=1323</guid>

					<description><![CDATA[<p>Everything we did in the last articles of the series SQL-Tutorial was a dry run because we just used SQLFiddle. So let&#8217;s start with a real database like SQLite. SQLite is a file based DBRMS and can be used for e.g. web sites. The official docs say: &#8220;SQLite works great as the database engine for&#8230;</p>
<p>The post <a href="https://creatronix.de/sqlite3-python-and-sql/">SQLite3: Python and SQL</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Everything we did in the last articles of the series <a href="https://creatronix.de/sql-tutorial/">SQL-Tutorial</a> was a dry run because we just used SQLFiddle.</p>
<p>So let&#8217;s start with a real database like SQLite.</p>
<p>SQLite is a file based DBRMS and can be used for e.g. web sites. The official docs say:</p>
<p><em>&#8220;SQLite works great as the database engine for most low to medium traffic websites (which is to say, most websites). [..] Generally speaking, any site that gets fewer than 100K hits/day should work fine with SQLite.&#8221;</em></p>
<p>Because Knight Industries is not Google, Amazon nor Facebook we can definitely use SQLite.</p>
<h2>Creating and connecting to a database</h2>
<p>In Python it is pretty easy to connect to a SQLite database:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>from sqlite3 import connect 
db_connection = connect('knight_industries.db')</code></pre>
</div>
<p>If the file knight_industries.db does not exist, it will be created automagically. A nice little feature of the sqlite3 library.</p>
<p>But be careful: If You already have a database file and you mess up the path in the connect statement you will wonder why you cannot access your data, because a new file is created silently.</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>cursor = db_connection.cursor()
cursor.execute('''CREATE TABLE operatives (id INTEGER, name TEXT, birthday DATE)''')
cursor.execute('''INSERT INTO operatives (id, name, birthday) \
                  VALUES (1, "Michael Arthur Long", "1949-01-09")''')

db_connection.commit()
cursor.execute('''SELECT * FROM operatives''')
print cursor.fetchone()
db_connection.close()</code></pre>
</div>
<p>&nbsp;</p>
<p>The post <a href="https://creatronix.de/sqlite3-python-and-sql/">SQLite3: Python and SQL</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://creatronix.de/sqlite3-python-and-sql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
