Elmer
Elmer

Reputation: 398

Pandas combine strings with groupby

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'])

Upvotes: 2

Views: 78

Answers (1)

jezrael
jezrael

Reputation: 862481

It seems you forget assign back, also was removed header=None in read_csv because in file is header used for columns names in DataFrame:

import pandas as pd

df = pd.read_csv("location.csv", sep=';', dtype='unicode', nrows=100)

df = df.groupby('code').agg('->'.join).reset_index()
print (df)
     code         text1
0  504273         text6
1  506141  text4->text5
2  507489  text2->text3

df.to_csv (r'new_location\export_dataframe.csv', index = False)

Upvotes: 2

Related Questions