Python
데이터프레임에 리스트를 행으로 넣기
다랭킴
2022. 2. 28. 01:01
방법1: 리스트 안에 행리스트의 꼴
# import pandas as pd
list1 = ['a', 'b','c']
list2 = ['d', 'e', 'f']
df = pd.DataFrame([list1, list2])
print(df)
# 0 1 2
#0 a b c
#1 d e f
방법2: Series로 만들어서 붙이기(안되는 경우가 있음)
원본 링크: https://emilkwak.github.io/dataframe-list-row-append-ignore-index
# import pandas as pd
df = pd.DataFrame()
list = ["a", "b, "c"]
df.append(pd.Series(list), ignore_index = True)
*안되는 경우 그 이유: append
list에 append()를 쓰면 list에 list가 추가되는 것이 아닌 전자의 list를 파괴해서 수정하는 개념에 가깝다고 한다.
참고한 링크: https://stackoverflow.com/questions/16641119/why-does-append-always-return-none-in-python
방법3: (=방법1) 리스트를 큰 리스트에 넣어서 칼럼 수로 자르기
simple_list=[['a','b']]
simple_list.append(['e','f'])
#simple_list = [['a','b'],['e','f']]가 된다.
df=pd.DataFrame(simple_list,columns=['col1','col2'])
# col1 col2
#0 a b
#1 e f