Answers for "how to add a column name in dataframe without losoing data"

2

add column names to dataframe pandas

# python 3.x
import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.random.randint(0, 10, (6,4)))

df.columns=["a", "b", "c", "d"]
print(df)
Posted by: Guest on July-04-2021
2

Adding new column to existing DataFrame in Pandas using assign method

# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]})

# print the DataFrame
print(df)

# append multiple columns to Pandas DataFrame
df2 = df.assign(lost=[2, 1, 3, 4], matches_remaining=[2, 3, 1, 1])

# Print the new DataFrame
print(df2)
Posted by: Guest on January-24-2022

Code answers related to "how to add a column name in dataframe without losoing data"

Python Answers by Framework

Browse Popular Code Answers by Language