I'm trying to combine a below code. Below code is working fine. I'm wondering if I can combine the below code to make it better and shorter because I have multiple set of dataframes where I need to append a seperate text to each set.
import pandas as pd
df_Gender = pd.DataFrame({'Col1': [40, 30, 20], 'COl2': [50, 10, 5]})
name = ['Sam']
lis = []
for i in name:
lis.append(i)
df = pd.DataFrame({'i': lis}) #Creating a dataframe to append the name
df_Gender = df_Gender.join(df) # joining name to the dataframe
Output:
Col1 COl2 name
40 50 Sam
30 10 Nan
20 5 Nan
wrote a separate syntax for the text repetition in a column
name = repeat('Sam')
df_Gender['i'] = [next(name for lis in range(len(df_Gender))]
Expected Output:
Col1 COl2 name
40 50 Sam
30 10 Sam
20 5 Sam