[python] pandas DataFrame으로 목록 목록 가져 오기
스프레드 시트의 내용을 팬더로 읽습니다. DataNitro에는 셀 목록을 사각형으로 선택하여 목록 목록으로 반환하는 방법이 있습니다. 그래서
table = Cell("A1").table
준다
table = [['Heading1', 'Heading2'], [1 , 2], [3, 4]]
headers = table.pop(0) # gives the headers as list and leaves data
나는 이것을 번역하기 위해 코드를 작성하는 데 바쁘지만 내 생각에는 그렇게하는 방법이 있어야하는 간단한 사용법이라고 생각합니다. 캔트가 문서에서 찾은 것 같습니다. 이것을 단순화하는 방법에 대한 조언이 있습니까?
답변
pd.DataFrame
생성자를 직접 호출하십시오 .
df = pd.DataFrame(table, columns=headers)
df
Heading1 Heading2
0 1 2
1 3 4
답변
위의 EdChum이 설명한 접근 방식으로 목록의 값이 행으로 표시됩니다. 대신 DataFrame에서 목록의 값을 열로 표시하려면 다음과 같이 transpose ()를 사용하십시오.
table = [[1 , 2], [3, 4]]
df = DataFrame(table)
df = df.transpose()
df.columns = ['Heading1', 'Heading2']
출력은 다음과 같습니다.
Heading1 Heading2
0 1 3
1 2 4
답변
pop
우리가 할 수있는 목록이 없어도set_index
pd.DataFrame(table).T.set_index(0).T
Out[11]:
0 Heading1 Heading2
1 1 2
2 3 4
최신 정보 from_records
table = [['Heading1', 'Heading2'], [1 , 2], [3, 4]]
pd.DataFrame.from_records(table[1:],columns=table[0])
Out[58]:
Heading1 Heading2
0 1 2
1 3 4