
# https://www.globalsino.com/ICs/page4853.html
# Split columns and then merge the splits in a csv file

import pandas as pd

df = pd.read_csv(r'C:\GlobalSino\ICs\ExampleFiledUsedINtestingPython\CityDate_with_space_in_cells.csv')
print(df)

# split all at "." in the "email" column
print('============ Split all at "." in the "email" column ==================')
print(df['email'].str.split('.'))

# split only one time at "." (namely split the first ".")
print('============ Split only one time at "." ==================')
print(df['email'].str.split('.', n=1))

# expand to new columns in the DataFrame
My_Split_Email = df['email'].str.split('.', n=1, expand=True)
My_Add = My_Split_Email.rename(columns={0:'Frist_Name', 1:'Last_Name'})

# "regex=True" is to avoid a message "FutureWarning: The default value of regex will change from True to False in a future version."
My_Add['Last_Name'] = My_Add['Last_Name'].str.replace('@gmail.com', '', regex=True)

My_Final = pd.concat([df, My_Add], axis=1)

print('============ Final ==================')
print(My_Final)
My_Final.to_csv(r'C:\GlobalSino\ICs\ExampleFiledUsedINtestingPython\Splitted_CityDate_with_space_in_cells.csv', index=False, header=True)

