๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Python/Pandas

[Python] Pandas - Dataframe ํ•จ์ˆ˜ ๋ชจ์Œ

๋ฐ˜์‘ํ˜•

 

merge

result_df = pd.merge(df1, df2, how='left', on='key')

result_df = pd.merge(df1, df2, how='left', left_on='name1', right_on='name2')

 

drop column

df.drop(columns=['datetime'], inplace=True)

 

reset index

df.reset_index(inplace=True, drop=True)

 

new col with apply

df['new_col'] = df.apply(lambda x: json.loads(x['loc'])['c'][1], axis=1)

 

sort

# ์—ด ์ด๋ฆ„ ์ˆœ์„œ axis=1

# ๋‚ด๋ฆผ์ฐจ์ˆœ ascending=False

df.sort_values(by=['datetime'], inplace=True, ascending=True)

 

rename col

df = df.rename(columns={'name' : 'new_name'})

 

string replace

df['name'] = df['name'].str.replace('a', 'b')

 

to_sql ์ด์šฉํ•œ Bulk insert

  • [์ถœ์ฒ˜] https://tzara.tistory.com/119
  • if_exists
    - fail : ํ…Œ์ด๋ธ” ์žˆ์„ ๊ฒฝ์šฐ ์˜ค๋ฅ˜, ์—†์„ ๊ฒฝ์šฐ ํ…Œ์ด๋ธ” ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๊ณ  insert
    - replace : ํ…Œ์ด๋ธ” drop, ์žฌ ์ƒ์„ฑ ํ›„ insert
    - append : ๊ธฐ์กด ํ…Œ์ด๋ธ”์— insert

def pg_connect():

    url = 'postgresql://postgres:pw@localhost:5432/postgres'

    engine = create_engine(url, client_encoding='utf8', use_batch_mode=True)

    return engine

 

pg_engine = pg_connect()

df.to_sql('target_table', con=pg_engine, if_exists='append', chunksize=1000, index=False)

 

์ฒซ๋ฒˆ์งธ ํ–‰์„ columns ์œผ๋กœ

df.rename(columns=df.iloc[0]) df.drop(df.index[0])

ใ…”ใ…›์†Œ

๋ฐ˜์‘ํ˜•