PYTHON (PANDAS) Concatenate content of two rows on the last of these two rows
Question:
I’m struggling on concatenations of content of two rows into the last of these two rows.
Here my situation:
A
B
C
D
NaN
NaN
Rossi
Brambilla
Federico
Giovanni
Giorgio
Marcello
I would like something like
A
B
C
D
NaN
NaN
Rossi
Brambilla
Federico
Giovanni
Rossi Giorgio
Brambilla Marcello
Could you please help me to reach this result?
Thanks in advance!
I tried the following:
df.iloc[1] = df_bo.iloc[0] + " " + df_bo.iloc[1]
but it gives me the following
IndexError: single positional indexer is out-of-bounds
I tried to transpose DF too, but it’s seems to be a bit sophisticated.
Answers:
You can use stack
to get rid of the NaNs, then groupby.agg
to join the names:
df.iloc[1] = df.stack().groupby(level=1).agg(' '.join)
If you want to limit the input rows:
df.iloc[1] = df.iloc[:2].stack().groupby(level=1).agg(' '.join)
Output:
A B C D
0 NaN NaN Rossi Brambilla
1 Federico Giovanni Rossi Giorgio Brambilla Marcello
If the original dataframe is df, this should work. 🙂
df2 = pd.concat([df.iloc[:1, :], pd.DataFrame(df.T[0] + ' ' + df.T[1]).T])
df1.loc[1,["C","D"]]=df1.loc[:,["C","D"]].agg(" ".join).tolist()
df1
out
A B C D
0 NaN NaN Rossi Brambilla
1 Federico Giovanni Rossi Rossi Giorgio Brambilla Brambilla Marcello
I’m struggling on concatenations of content of two rows into the last of these two rows.
Here my situation:
A | B | C | D |
---|---|---|---|
NaN | NaN | Rossi | Brambilla |
Federico | Giovanni | Giorgio | Marcello |
I would like something like
A | B | C | D |
---|---|---|---|
NaN | NaN | Rossi | Brambilla |
Federico | Giovanni | Rossi Giorgio | Brambilla Marcello |
Could you please help me to reach this result?
Thanks in advance!
I tried the following:
df.iloc[1] = df_bo.iloc[0] + " " + df_bo.iloc[1]
but it gives me the following
IndexError: single positional indexer is out-of-bounds
I tried to transpose DF too, but it’s seems to be a bit sophisticated.
You can use stack
to get rid of the NaNs, then groupby.agg
to join the names:
df.iloc[1] = df.stack().groupby(level=1).agg(' '.join)
If you want to limit the input rows:
df.iloc[1] = df.iloc[:2].stack().groupby(level=1).agg(' '.join)
Output:
A B C D
0 NaN NaN Rossi Brambilla
1 Federico Giovanni Rossi Giorgio Brambilla Marcello
If the original dataframe is df, this should work. 🙂
df2 = pd.concat([df.iloc[:1, :], pd.DataFrame(df.T[0] + ' ' + df.T[1]).T])
df1.loc[1,["C","D"]]=df1.loc[:,["C","D"]].agg(" ".join).tolist()
df1
out
A B C D
0 NaN NaN Rossi Brambilla
1 Federico Giovanni Rossi Rossi Giorgio Brambilla Brambilla Marcello