MINERVA/Python
2023. 4. 4. 15:34
반응형
간단하게 샘플 코드로 정리하고자 합니다.
import os
import pandas as pd
import numpy as np
if __name__ == '__main__':
print(f'{os.path.dirname(__file__)}')
#
df = pd.DataFrame( np.random.randn(5, 5),
columns=['A','B','C','D','E' ] )
# show all data
print(df)
# print #2 row
print(df.loc[2, :])
# Dataframe to List
rowList = df.loc[2, :].values.flatten().tolist()
print(rowList)
# subset dataframe to list
rowList = df.loc[2, ['A','E']].values.flatten().tolist()
print(rowList)
[실행결과]
# show all data
print(df)
A B C D E
0 1.736102 -1.477961 -1.179571 1.411220 -0.211700
1 0.175911 -0.518549 0.594174 0.333632 -1.579409
2 0.595619 -0.224543 -0.715213 -0.049334 -0.471737
3 0.639203 -0.451774 -0.009238 0.684090 -0.036995
4 -0.303029 -0.655539 -1.132769 0.485218 1.205955
# print #2 row
print(df.loc[2, :])
A 0.595619
B -0.224543
C -0.715213
D -0.049334
E -0.471737
Name: 2, dtype: float64
# Dataframe to List
rowList = df.loc[2, :].values.flatten().tolist()
print(rowList)
[0.5956188992136562, -0.2245426559477047, -0.7152127642656871, -0.049333965859220306, -0.4717365476151358]
# subset dataframe to list
rowList = df.loc[2, ['A','E']].values.flatten().tolist()
print(rowList)
[0.5956188992136562, -0.4717365476151358]
반응형