'Python3 add prefix on specific column with different condition
I have question on python about replacing series of prefix on column with few conditions. Please find sample code as below: if value in column 0 contain/like 'VCD00%' or'ADC00%' then add prefix '331487s' if contain/like 'ADE01' then add prefix '00695B' else leave it as original value:
import numpy as np
import pandas as pd
data = pd.read_csv (r'C:\file\file.csv', index_col=None, usecols=[0,1])
if(data.empty):
print ('CSV file is empty')
else:
data.dropna()
df = pd.DataFrame(data)
df[0] = pd.np.where(df[0].str.contains("VDC00"),'331487S'+df[0].astype(str),
pd.np.where(df[0].str.contains('ADC00'), '331487S'+df[0].astype(str),
pd.np.where(df[0].str.contains('ADE01'), '00695B'+df[0].astype(str))))
print(df)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|