xxxxxxxxxx
# importing pandas as pd
import pandas as pd
# Creating the DataFrame
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'],
'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
'Cost':[10000, 5000, 15000, 2000]})
# Print the dataframe
print(df)
xxxxxxxxxx
new_df = df.filter(like='n_') \
.replace(0., np.inf) \
.apply(lambda x: sorted(x), axis=1, result_type='expand') \
.replace(np.inf, 0.0)
new_df.columns = ['new_1', 'new_2', 'new_3']
out = pd.concat([df, new_df], axis=1)
xxxxxxxxxx
import pandas as pd
# Create a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6]}
df = pd.DataFrame(data)
# Create a new column 'C' by summing values from columns 'A' and 'B'
df['C'] = df['A'] + df['B']
# Print the updated DataFrame
print(df)
xxxxxxxxxx
# 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)