본문 바로가기
Python

[Python] Append Row to pandas DataFrame

by 소여물리에 2022. 12. 12.

 

 

# Append Row to DataFrame
list_row = ["Hyperion", 27000, "60days", 2000]
df.loc[len(df)] = list_row

# Insert Dict to the dataframe using DataFrame.append()
new_row = {'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}
df2 = df.append(new_row, ignore_index=True)

# Append new row to specifig index name
df2 = df.append(pd.DataFrame([new_row],index=['7'],columns=df.columns))

# Append row to the DataFrame
df2 = df.append(pd.Series(new_row, index=df.columns, name='7'))

# Using pandas.concat() to append a row
new_row = pd.DataFrame({'Courses':'Hyperion', 'Fee':24000, 'Duration':'55days', 'Discount':1800}, index=[0])
df2 = pd.concat([new_row,df.loc[:]]).reset_index(drop=True)

# Append specific row/index name using DataFrame.loc[]
df.loc['7', :] = ['Hive',25000,'45days',2000]

# Append row in DataFrame using DataFrame.loc[]
df.loc['7'] = ['Hive',25000,'45days',2000]

 

 

 

 

 

 

 

 

반응형