Python Pandas Module Cheatsheet: Unterschied zwischen den Versionen

Aus MattWiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 31: Zeile 31:


</syntaxhighlight>
</syntaxhighlight>
== Viewing Dataframes ==
=== Show top lines ===
<syntaxhighlight lang="python3">
print(df.head())    # print first 5 lines
print(df.head(10)    # print first 10 lines
</syntaxhighlight>
=== Get Informations about Dataframe Data ===
<syntaxhighlight lang="python3">
print(df.info())
</syntaxhighlight>Prints Number of records, and name, datatype and number of filled lines of each individual column.
[[Kategorie:Python]]
[[Kategorie:Python]]

Version vom 2. März 2025, 21:15 Uhr

Import Pandas Module

import pandas as pd

Creating Dataframes

From a Dictionary

df1 = pd.DataFrame({
    'name': ['John Smith', 'Jane Doe', 'Joe Schmo'],
    'address': ['123 Main St.', '456 Maple Ave.', '789 Broadway'],
    'age': [34, 28, 51]
})

From a List

df2 = pd.DataFrame([
    ['John Smith', '123 Main St.', 34],
    ['Jane Doe', '456 Maple Ave.', 28],
    ['Joe Schmo', '789 Broadway', 51]
    ],
    columns=['name', 'address', 'age'])

From a CSV File

df3 = pd.read_csv('sample.csv')

Viewing Dataframes

Show top lines

print(df.head())     # print first 5 lines
print(df.head(10)    # print first 10 lines

Get Informations about Dataframe Data

print(df.info())

Prints Number of records, and name, datatype and number of filled lines of each individual column.