I wrote a code that takes value in cell, changes it and the it should replace value in that cell with new value. I have .replace function fit dummy dataframe, and it works, but for my example it does not work.
Old value and new value are very long strings. I have tried with inplace=True and without it.
My goal is to change ServiceDirection value.
You can download the data from here:
https://www.sendspace.com/file/7soufd
old value looks like this:
...ype=1|ServiceDirection=2|CmtsMdIfIn...
new value looks like this:
ype=1|ServiceDirection=DS|CmtsMdIfIn
This is the code:
data = pd.read_csv('data.csv')
def third_task():
new_data = data
for column in data:
for row in data[column]:
if 'ServiceDirection=1' in str(row):
new_row = str(row).replace('ServiceDirection=1', 'ServiceDirection=DS')
new_data = data.replace(str(row), new_row)
elif 'ServiceDirection=2' in str(row):
new_row = str(row).replace('ServiceDirection=2', 'ServiceDirection=US')
new_data = data.replace(str(row), new_row)
export_csv = new_data.to_csv(r'C:\Users\Pc\Desktop\export_dataframe1.csv', index = None, header=False)
return new_data
print(third_task())
I have also tried to do this:
df.replace(row, result)
Instead of this:
data[column] = data[column].replace(str(row), str(result), inplace=True)
But still does not work, it always returns dataframe with old values
for row in data['column']