Skip to content

Commit 4cbfc01

Browse files
committed
<add>: pd manipulation
1 parent 7752e05 commit 4cbfc01

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

PandasPractice/pd.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
import pandas as pd
22
'''
3-
# python dictionary obj to pd.Series obj
3+
# python dict to pd.Series
44
dict_data = {'song_name': 'STAY', 'artist': 'Bustin Jieber', 'release_date': 20220808}
55
66
sr = pd.Series(dict_data)
77
print(type(sr))
8-
print('\n')
98
print(sr) ## dtype: object
109
10+
idx = sr.index
11+
val = sr.values
12+
print(idx)
13+
print(val)
14+
print(type(val)) ## numpy.ndarray
15+
print(type(idx)) ## pandas.core.indexes.base.Index
16+
'''
17+
18+
'''
19+
python dict to pd.Series
1120
dict_data2 = {'a': 1, 'b': 2, '3': 20220808}
1221
sr = pd.Series(dict_data2)
1322
print(type(sr))
1423
print('\n')
1524
print(sr) ## dtype: int64
25+
'''
1626

1727

28+
'''
29+
# python dict to pd.Series
1830
dict_data3 = {'a': 'apple', 'b': 'bile', 'c': 'cyclone'}
1931
sr = pd.Series(dict_data3)
2032
print(type(sr))
@@ -25,14 +37,48 @@
2537
print(idx)
2638
print(val)
2739
print(type(val)) ## numpy.ndarray
40+
print(type(idx)) ## pandas.core.indexes.base.Index
2841
'''
2942

30-
# python tuple obj to pd.Series obj
43+
44+
'''
45+
# python tuple to pd.Series
3146
tuple_data = ('음성군', '진천읍', True, 4412)
3247
sr = pd.Series(tuple_data, ['first_loc', 'second_loc', 'including', '_id'])
33-
print(sr)
48+
#print(sr)
3449
3550
# slicing , choosing elements.
51+
sr[0]
52+
sr['first_loc']
53+
'''
3654

55+
'''
56+
# {key:list as value} to Dataframe
57+
dict_data = {'b1_34': ['24가1023'], 'b1_35': ['12가0734'], 'b2_22': ['09이2831', '28푸2122']}
58+
# all arrays must be of the same length
59+
dict_data = {'b1_34': ['24가1023'], 'b1_35': ['12가0734'], 'b2_22': ['09이2831']}
60+
df = pd.DataFrame(dict_data) # 행 인덱스에는 정수형 위치 인덱스가 자동 지정된다.
61+
idx = df.index
62+
print(df)
63+
print(type(df))
64+
print(idx) # RangeIndex(start=0, stop=1, step=1)
65+
print(type(idx)) # pandas.core.indexes.range.RangeIndex
66+
'''
3767

68+
'''
69+
# two-dimensional python list to Dataframe
70+
df = pd.DataFrame([[22,'남','덕영중'], [17,'여','수리중']], index=['학생1','학생2'], columns=['age','성별','학교'])
71+
print(df)
72+
print(df.index)
73+
print(type(df.index)) # pandas.core.indexes.base.Index
74+
print(df.columns)
75+
print(type(df.columns)) # pandas.core.indexes.base.Index
3876
77+
df.index = ['참가자1', '참가자2'] # '참가자3' 추가시 ValueError: Length mismatch
78+
df.columns = ['나이', 'gender', '소속']
79+
print(df)
80+
print(df.index)
81+
print(type(df.index)) # pandas.core.indexes.base.Index
82+
print(df.columns)
83+
print(type(df.columns)) # pandas.core.indexes.base.Index
84+
'''

0 commit comments

Comments
 (0)