본문 바로가기
파이썬

파이썬 Pandas를 사용하여 데이터 프레임에 빈 행 추가

by º기록 2020. 10. 28.
반응형

나는 데이터 프레임의 끝에 빈 행을 추가하려고 시도하고 있지만 그렇게 할 수는 없지만 팬더가 추가 기능으로 작동하는 방식을 이해하려고 노력하지만 여전히 얻지 못합니다.

코드는 다음과 같습니다.

import pandas as pd

excel_names = ["ARMANI+EMPORIO+AR0143-book.xlsx"]
excels = [pd.ExcelFile(name) for name in excel_names]
frames = [x.parse(x.sheet_names[0], header=None,index_col=None).dropna(how='all') for x in excels]
for f in frames:
    f.append(0, float('NaN'))
    f.append(2, float('NaN'))

두 개의 열과 임의의 행이 있습니다.

for 루프에서 "print f"로 i Get this :

                             0                 1
0                   Brand Name    Emporio Armani
2                 Model number            AR0143
4                  Part Number            AR0143
6                   Item Shape       Rectangular
8   Dial Window Material Type           Mineral
10               Display Type          Analogue
12                 Clasp Type            Buckle
14               Case Material   Stainless steel
16              Case Diameter    31 millimetres
18               Band Material           Leather
20                 Band Length  Women's Standard
22                 Band Colour             Black
24                 Dial Colour             Black
26            Special Features       second-hand
28                    Movement            Quartz

 

해결 방법

 

pandas.DataFrame.append ()를 사용하여 새 pandas.Series를 추가합니다.

새 행의 이름 (일명 "인덱스")을 지정하려면 다음을 사용하십시오.

df.append(pandas.Series(name='NameOfNewRow'))

새 행의 이름을 지정하지 않으려면 다음을 사용하십시오.

df.append(pandas.Series(), ignore_index=True)

여기서 df 는 pandas.DataFrame입니다.

 

참조 페이지 https://stackoverflow.com/questions/39998262

 

 

반응형

댓글