0

I need some help about Pandas

   model        timestamp
0  Punto  20200124_083155
1  Punto  20200124_163540
2  Doblo  20200124_122052
3  Doblo  20200124_150801
4  Panda  20200124_134350
5   Tipo  20200124_195955

I want it become to

   model        timestamp
0  Punto  20200124_083155;20200124_163540
1  Doblo  20200124_122052;20200124_150801  
2  Panda  20200124_134350
3  Tipo   20200124_195955

Please, everybody help me, can give me some example?, thank so much.

2
  • Why would you want that? It's more difficult to manipulate. Commented Apr 21, 2022 at 3:25
  • My teacher asked for such data analysis, if you know can u give me some example? please Commented Apr 21, 2022 at 3:30

2 Answers 2

1

You can use agg for that and concatenate the grouped data.

new_df = df.groupby('model')['timestamp'].agg(timestamp= lambda x: ';'.join(x))

print(new_df)
                             timestamp
model                                 
Doblo  20200124_122052;20200124_150801
Panda                  20200124_134350
Punto  20200124_083155;20200124_163540
Tipo                   20200124_195955

new_df.reset_index(inplace=True)
print(new_df)

   model                        timestamp
0  Doblo  20200124_122052;20200124_150801
1  Panda                  20200124_134350
2  Punto  20200124_083155;20200124_163540
3   Tipo                  20200124_195955

Sign up to request clarification or add additional context in comments.

2 Comments

.reset_index() at the end to get exactly the output requested~
I was just about to edit it. noticed after posting the answer:)
0

Suppose you have:

df_new=pd.DataFrame()
df_new['name']=['Punto','Doblo','Doblo','Punto','Punto','Tipo']
df_new['date']=['20200124_083155','20200124_163540','20200124_122052','20200124_150801',
'20200124_134350','20200124_195955']

output:

name    date
0   Punto   20200124_083155
1   Doblo   20200124_163540
2   Doblo   20200124_122052
3   Punto   20200124_150801
4   Punto   20200124_134350
5   Tipo    20200124_195955

Use shift with groupby:

df_new['date2']=df_new.groupby('name').shift(-1)
df_new['date2']=';'+df_new['date2']
df_new['date2']=df_new['date2'].fillna('')

output:

name    date    date2
0   Punto   20200124_083155 ;20200124_150801
1   Doblo   20200124_163540 ;20200124_122052
2   Doblo   20200124_122052 
3   Punto   20200124_150801 ;20200124_134350
4   Punto   20200124_134350 
5   Tipo    20200124_195955 

if you want to see it in the same column:

df_new['date3']=df_new['date']+df_new['date2']

output:

  name  date    date2   date3
0   Punto   20200124_083155 ;20200124_150801    20200124_083155;20200124_150801
1   Doblo   20200124_163540 ;20200124_122052    20200124_163540;20200124_122052
2   Doblo   20200124_122052     20200124_122052
3   Punto   20200124_150801 ;20200124_134350    20200124_150801;20200124_134350
4   Punto   20200124_134350     20200124_134350
5   Tipo    20200124_195955     20200124_195955

It is not concise. But this approach will ensure that you only have two dates in the column. Unlike with groupby and transform method

2 Comments

THanks you, but I need more about if it not 2 which is 3 or 4 or ... Can u give me some example?
I have modified my answer. It is applicable if you want to have two dates only in the column ('date3').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.