【PYTHON】Loading and Saving Data with Pandas

 pip install pygments


!pygmentize -l text itunes_data.csv


import pandas as pd


csv_df = pd.read_csv('itunes_data.csv')

csv_df.head()


excel_df = pd.read_excel('itunes_data.xlsx', engine='openpyxl')

excel_df.head()


from sqlalchemy import create_engine

engine = create_engine('sqlite:///chinook.db')


query = """SELECT tracks.name as Track, tracks.composer, tracks.milliseconds,

tracks.bytes, tracks.unitprice,

genres.name as Genre,

albums.title as Album,

artists.name as Artist

FROM tracks

JOIN genres ON tracks.genreid = genres.genreid

JOIN albums ON tracks.albumid = albums.albumid

JOIN artists ON albums.artistid = artists.artistid;

"""


with engine.connect() as connection:

  sql_df = pd.read_sql_query(query, connection)


sql_df.head(2).T


# create dataframe from lists

df = pd.DataFrame(data={'seconds': [1, 2, 3, 4], 'intensity': [12, 11, 12, 14]})

df.head()


sql_df.index


sql_df.columns


type(sql_df)


itunes_df = pd.concat([csv_df, excel_df, sql_df], axis=0)

itunes_df.head()

itunes_df.tail()


print(itunes_df.iloc[0])

print(itunes_df.iloc[-1])


itunes_df.iloc[0, 0]


itunes_df.iloc[-1, -1]


itunes_df.loc[3502]


test_df = itunes_df.copy()

test_df = test_df.append(itunes_df.loc[3502])

test_df.loc[3502]

No comments:

Power Is Shifting Rapidly to Indie Creators

The exodus from Twitter since the election is a sign of things to come ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ...

Contact Form

Name

Email *

Message *