Answers for "function to add a row to dataframe python"

25

add rows to dataframe pandas

df = df.append({'a':1, 'b':2}, ignore_index=True)
Posted by: Guest on September-21-2020
1

dataframe add row

# append row to dataframe without index

a_row = pd.Series([1, 2])
df = pd.DataFrame([[3, 4], [5, 6]])

row_df = pd.DataFrame([a_row])
df = pd.concat([row_df, df], ignore_index=True)

print(df)
# OUTPUT
#    0  1
# 0  1  2
# 1  3  4
# 2  5  6

# append row to dataframe with index

a_row = pd.Series([1, 2])
df = pd.DataFrame([[3, 4], [5, 6]], index = ["row1", "row2"])

row_df = pd.DataFrame([a_row], index = ["row3"])
df = pd.concat([row_df, df])

print(df)
# OUTPUT
#       0  1
# row3  1  2
# row1  3  4
# row2  5  6
Posted by: Guest on August-10-2021

Code answers related to "function to add a row to dataframe python"

Python Answers by Framework

Browse Popular Code Answers by Language