I am trying to combine strings in my data frame. The dataframe looks like:
0 code text1
1 507489 text2
2 507489 text3
3 506141 text4
4 506141 text5
5 504273 text6
My current code:
import pandas as pd
df = pd.read_csv("location.csv", header=None, delimiter=';', dtype='unicode', nrows=100)
new_header = df.iloc[0]
df = df[1:]
df.columns = new_header
df.groupby('code').agg('->'.join).reset_index()
df.to_csv (r'new_location\export_dataframe.csv', index = False, header=True)
print(df)
But I am not getting the expected results. The output looks the same as the input while I was expecting:
0 code text1
1 507489 text2->text3
2 506141 text4->text5
3 504273 text6
Quite new to this so I must be making some easy mistake.
Dataframe that produces same result:
testf = {'code': ['1','2','2','4'],
'text': [22000,25000,27000,35000]
}
df = pd.DataFrame(testf, columns = ['code', 'text'])
DFthat will can run , don't forget we don't have your csv, and that will make time to implement youDFyou have to follow this step before stackoverflow.com/help/minimal-reproducible-example