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])
ใ ใ ์
'Python > Pandas' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Pandas] Dataframe ์ ์ฒด ์ถ๋ ฅํ๊ธฐ (0) | 2023.10.18 |
---|---|
[Pandas] Dataframe ์์์ ๊ด๋ จ (0) | 2022.10.31 |
[Python] Pandas - Dataframe for ๋ฌธ ์กฐํ (0) | 2021.02.16 |
[Python] Pandas - DataFrame ์ด์์น ์ ๊ฑฐ (0) | 2020.12.23 |
[Python] Pandas - DataFrame ํน์ ์ด ์ ํ (0) | 2020.12.18 |