Answers for "add value to row in dataframe"

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
3

how to add a row to a dataframe in python

import pandas as pd

data = {'name': ['Somu', 'Kiku', 'Amol', 'Lini'],
	'physics': [68, 74, 77, 78],
	'chemistry': [84, 56, 73, 69],
	'algebra': [78, 88, 82, 87]}

	
#create dataframe
df_marks = pd.DataFrame(data)
print('Original DataFrame\n------------------')
print(df_marks)

new_row = {'name':'Geo', 'physics':87, 'chemistry':92, 'algebra':97}
#append row to the dataframe
df_marks = df_marks.append(new_row, ignore_index=True)

print('\n\nNew row added to DataFrame\n--------------------------')
print(df_marks)
Posted by: Guest on February-23-2020

Python Answers by Framework

Browse Popular Code Answers by Language