๐๐๐ก๐๐๐๐๐
fomatting, split, group by, pivot_table ๋ณธ๋ฌธ
[ ๋ชฉ์ฐจ ]
- ํฌ๋งทํ (formatting)
- split
- (ํน์ ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก)
- ๋ฆฌ์คํธ์ ๊ฐ ํญ๋ชฉ์ ๋ฌธ์์ด๋ก (์๊ณ ๋ง ์๋ ๊ฑธ๋ก)
- ๋ฆฌ์คํธ์ ๊ฐ ํญ๋ชฉ์ ๋ฌธ์์ด๋ก ๊ฒฐํฉํ๋, ํน์ ๊ตฌ๋ถ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐํฉ
- ์ฌ๋ฌ ์ค๋ก ์ด๋ฃจ์ด์ง ๋ฌธ์์ด์ ์ค ๋จ์๋ก ๋ถํ ํ์ฌ ๋ฆฌ์คํธ๋ก ๋ณํํ๊ธฐ
- ๋ฌธ์์ด์ ๊ณต๋ฐฑ์ผ๋ก ๋ถํ ํ ํ ํน์ ๊ฐ์์ ํญ๋ชฉ๋ง ๊ฐ์ ธ์ค๊ธฐ
- ๋ฌธ์์ด์์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ ํ ๋ฌธ์์ด์ ๋ฆฌ์คํธ๋ก ๋ณํํ๊ธฐ
- split ์ค์ ์์
- group by()
- pivot_table()
ํฌ๋งทํ (formatting)
f-string
x = 10
print(f"๋ณ์ x์ ๊ฐ์ {x}์
๋๋ค.")
x = 10
print("๋ณ์ x์ ๊ฐ์ {}์
๋๋ค.".format(x))
x = 10
print("๋ณ์ x์ ๊ฐ์ %d์
๋๋ค." % (x))
split
๋ฌธ์์ด โ ๋ฆฌ์คํธ๋ก ๋ณํ (๊ณต๋ฐฑ ๊ธฐ์ค์ผ๋ก ๋ถํ )
sentence = "Hello, how are you doing today?"
words = sentence.split()
print(words) # ์ถ๋ ฅ: ['Hello,', 'how', 'are', 'you', 'doing', 'today?']
(ํน์ ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก)
data = "apple,banana,grape,orange"
fruits = data.split(',')
print(fruits) # ์ถ๋ ฅ: ['apple', 'banana', 'grape', 'orange']์ฝ๋๋ฅผ ์
๋ ฅํ์ธ์
๋ฆฌ์คํธ์ ๊ฐ ํญ๋ชฉ์ ๋ฌธ์์ด๋ก (์๊ณ ๋ง ์๋ ๊ฑธ๋ก)
words = ['Hello,', 'how', 'are', 'you', 'doing', 'today?']
sentence = ' '.join(words)
print(sentence) # ์ถ๋ ฅ: Hello, how are you doing today?
๋ฆฌ์คํธ์ ๊ฐ ํญ๋ชฉ์ ๋ฌธ์์ด๋ก ๊ฒฐํฉํ๋, ํน์ ๊ตฌ๋ถ์๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฐํฉ
fruits = ['apple', 'banana', 'grape', 'orange']
data = ','.join(fruits)
print(data) # ์ถ๋ ฅ: apple,banana,grape,orange
์ฌ๋ฌ ์ค๋ก ์ด๋ฃจ์ด์ง ๋ฌธ์์ด์ ์ค ๋จ์๋ก ๋ถํ ํ์ฌ ๋ฆฌ์คํธ๋ก ๋ณํํ๊ธฐ
text = """First line
Second line
Third line"""
lines = text.split('\n')
print(lines) # ์ถ๋ ฅ: ['First line', 'Second line', 'Third line']
๋ฌธ์์ด์ ๊ณต๋ฐฑ์ผ๋ก ๋ถํ ํ ํ ํน์ ๊ฐ์์ ํญ๋ชฉ๋ง ๊ฐ์ ธ์ค๊ธฐ
sentence = "Hello, how are you doing today?"
words = sentence.split()
first_three_words = words[:3]
print(first_three_words) # ์ถ๋ ฅ: ['Hello,', 'how', 'are']
๋ฌธ์์ด์์ ๊ณต๋ฐฑ์ ์ ๊ฑฐํ ํ ๋ฌธ์์ด์ ๋ฆฌ์คํธ๋ก ๋ณํํ๊ธฐ
text = " Hello how are you "
cleaned_text = text.strip()
words = cleaned_text.split()
print(words) # ์ถ๋ ฅ: ['Hello', 'how', 'are', 'you']
split ์ค์ ์์
# ๋ฐ์ดํฐ์ ๊ฒฝ๋ก๋ฅผ ๋ฌธ์์ด๋ก ํํ
file_path = "/usr/local/data/sample.txt"
# split() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋๋ ํ ๋ฆฌ์ ํ์ผ๋ช
์ผ๋ก ๋ถํ
directory, filename = file_path.rsplit('/', 1)
print("๋๋ ํ ๋ฆฌ:", directory) # ์ถ๋ ฅ: ๋๋ ํ ๋ฆฌ: /usr/local/data
print("ํ์ผ๋ช
:", filename) # ์ถ๋ ฅ: ํ์ผ๋ช
: sample.txt
- ์์์์๋ file_path๋ผ๋ ๋ฌธ์์ด ๋ณ์์ ๋ฐ์ดํฐ์ ๊ฒฝ๋ก๋ฅผ ์ ์ฅํ๊ณ , split() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌธ์์ด์ / ๊ธฐ์ค์ผ๋ก ๋ถํ ํฉ๋๋ค.
- ์ด๋, rsplit() ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ค๋ฅธ์ชฝ์์๋ถํฐ ์ต๋ 1ํ๋ง ๋ถํ ํ๋๋ก ์ค์ ํ์ฌ ํ์ผ๋ช ๊ณผ ๋๋ ํ ๋ฆฌ๋ก ๋๋๋๋ค.
- ๋ถํ ๋ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ๊ฐ directory์ filename ๋ณ์์ ํ ๋นํ์ฌ ์ถ๋ ฅํฉ๋๋ค.
group by()
- ๋ฐ์ดํฐ๋ฅผ ํผ๋ดํ ํ์ฌ ํต๊ณ๋์ ๋ณผ ์ ์๋๋ก ๋์์ฃผ๋ ๋ฉ์๋
- ํน์ ์กฐ๊ฑด์ ๋ง๊ฒ ์ ์ฒ๋ฆฌํด ์ค ๋ ์ฉ์ด
by : ๊ทธ๋ฃนํํ ๋ด์ฉ. ํจ์, ์ถ, ๋ฆฌ์คํธ ๋ฑ๋ฑ์ด ์ฌ ์ ์์
sort : ๊ทธ๋ฃนํค๋ฅผ ์ ๋ ฌํ ์ง ์ฌ๋ถ
dropna : ๊ฒฐ์ธก๊ฐ์ ๊ณ์ฐ์์ ์ ์ธํ ์ง ์ฌ๋ถ
# ๋ค์ค ์ปฌ๋ผ groupby
df.groupby(['sex', 'pclass'])[['survived', 'age']].mean()
# ๋ค์ค ํต๊ณ๊ฐ
df.groupby(['sex', 'pclass'])[['survived', 'age']].agg(['mean', 'sum'])
pivot_table()
- ๋ฐ์ดํฐ๋ฅผ ์คํ๋ ๋์ํธ ๊ธฐ๋ฐ ํผ๋ฒ ํ ์ด๋ธ๋ก ๋ณํํ๋ ๋ฉ์๋, ์์ ์คํ๋ ๋์ํธ ํผ๋ฒ ํ ์ด๋ธ๊ณผ ์ ์ฌ
values : ๊ฐ์ผ๋ก ์
๋ ฅ๋ ์ปฌ๋ผ
aggfunc : ์ ์ฉํ ํจ์
fill_value : ๊ฒฐ์ธก์น๋ฅผ ์ฑ์๋ฃ์ ๊ฐ
# index์ ๊ทธ๋ฃน์ ํ๊ธฐ
df.pivot_table(index='who', values='survived')
# columns์ ๊ทธ๋ฃน์ ํ๊ธฐ
df.pivot_table(columns='who', values='survived')
df.pivot_table(index=['who', 'pclass'], values='survived')
df.pivot_table(index='who',
columns='pclass',
values='survived',
aggfunc=['sum', 'mean'])
'ํ์ด์ฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ฌ ๊ธฐ์ด (1) | 2025.01.10 |
---|