concatenate pandas rows if next row has NaN in specific columnAdd one row to pandas DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column nameHow to drop rows of Pandas DataFrame whose value in certain columns is NaNHow do I get the row count of a Pandas dataframe?How to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
Did 'Cinema Songs' exist during Hiranyakshipu's time?
Could the museum Saturn V's be refitted for one more flight?
Unlock My Phone! February 2018
What are the G forces leaving Earth orbit?
How exploitable/balanced is this homebrew spell: Spell Permanency?
Why was Sir Cadogan fired?
Sums of two squares in arithmetic progressions
How to stretch the corners of this image so that it looks like a perfect rectangle?
Knowledge-based authentication using Domain-driven Design in C#
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
Am I breaking OOP practice with this architecture?
Is it a bad idea to plug the other end of ESD strap to wall ground?
How do I exit BASH while loop using modulus operator?
Standard deduction V. mortgage interest deduction - is it basically only for the rich?
Send out email when Apex Queueable fails and test it
Label inside tikzcd square
When handwriting 黄 (huáng; yellow) is it incorrect to have a disconnected 草 (cǎo; grass) radical on top?
Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?
Mathematica command that allows it to read my intentions
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
What is the opposite of "eschatology"?
Can I hook these wires up to find the connection to a dead outlet?
Why do I get negative height?
What is the most common color to indicate the input-field is disabled?
concatenate pandas rows if next row has NaN in specific column
Add one row to pandas DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame by column nameHow to drop rows of Pandas DataFrame whose value in certain columns is NaNHow do I get the row count of a Pandas dataframe?How to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers
I have csv file parsed from pdf file and it is not correctly parsed as the table in pdf file has multiple rows.
importing it into pd DataFrame looks like this.
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, AAAARSBG
3 NaN REPUBLIC OF NaN
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA AAACKWKW
5 NaN OPPOSITE PUBLIC LIBRARY 13022 KUWAIT NaN
6 NaN KUWAIT NaN
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS AAADFRP1
8 NaN FRANCE NaN
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
10 NaN NaN NaN
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS AAAJBG21
12 NaN CENTER LEGIS 4000 PLOVDIV BULGARIA NaN
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 AAALSARI
14 NaN RIYADH SAUDI ARABIA NaN
I want to concatenate next rows into current row if next value in Record column is NaN.
I mean I want to get
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
here is dataframe
import numpy a np
data = 'Record': 2: '2007-03-03',
3: np.NaN,
4: '1994-03-07',
5: np.NaN,
6: np.NaN,
7: '2006-06-03',
8: np.NaN,
9: '2006-06-03',
10: np.NaN,
11: '2014-07-05',
12: np.NaN,
13: '1989-03-29',
14: np.NaN,
'Operational Address': 2: 'Omladinskih Brigada 90V 11070 BEOGRAD SERBIA,',
3: 'REPUBLIC OF',
4: 'SAFAT ALI AL SALEM STREET, MUBARAKIA',
5: 'OPPOSITE PUBLIC LIBRARY 13022 KUWAIT',
6: 'KUWAIT',
7: 'CHEZ NSM 3, AVENUE HOCHE 75008 PARIS',
8: 'FRANCE',
9: '10 RUE DU COLISEE 75008 PARIS FRANCE',
10: np.NaN,
11: '152, 6TH OF SEPTEMBER BLVD. BUSINESS',
12: 'CENTER LEGIS 4000 PLOVDIV BULGARIA',
13: 'DHABAB STREET HEAD OFFICE BUILDING 11431',
14: 'RIYADH SAUDI ARABIA',
'BIC': 2: 'AAAARSBG',
3: np.NaN,
4: 'AAACKWKW',
5: np.NaN,
6: np.NaN,
7: 'AAADFRP1',
8: np.NaN,
9: 'AAAGFRP1',
10: np.NaN,
11: 'AAAJBG21',
12: np.NaN,
13: 'AAALSARI',
14: np.NaN
df = pd.DataFrame(data=data)
python pandas dataframe
add a comment |
I have csv file parsed from pdf file and it is not correctly parsed as the table in pdf file has multiple rows.
importing it into pd DataFrame looks like this.
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, AAAARSBG
3 NaN REPUBLIC OF NaN
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA AAACKWKW
5 NaN OPPOSITE PUBLIC LIBRARY 13022 KUWAIT NaN
6 NaN KUWAIT NaN
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS AAADFRP1
8 NaN FRANCE NaN
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
10 NaN NaN NaN
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS AAAJBG21
12 NaN CENTER LEGIS 4000 PLOVDIV BULGARIA NaN
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 AAALSARI
14 NaN RIYADH SAUDI ARABIA NaN
I want to concatenate next rows into current row if next value in Record column is NaN.
I mean I want to get
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
here is dataframe
import numpy a np
data = 'Record': 2: '2007-03-03',
3: np.NaN,
4: '1994-03-07',
5: np.NaN,
6: np.NaN,
7: '2006-06-03',
8: np.NaN,
9: '2006-06-03',
10: np.NaN,
11: '2014-07-05',
12: np.NaN,
13: '1989-03-29',
14: np.NaN,
'Operational Address': 2: 'Omladinskih Brigada 90V 11070 BEOGRAD SERBIA,',
3: 'REPUBLIC OF',
4: 'SAFAT ALI AL SALEM STREET, MUBARAKIA',
5: 'OPPOSITE PUBLIC LIBRARY 13022 KUWAIT',
6: 'KUWAIT',
7: 'CHEZ NSM 3, AVENUE HOCHE 75008 PARIS',
8: 'FRANCE',
9: '10 RUE DU COLISEE 75008 PARIS FRANCE',
10: np.NaN,
11: '152, 6TH OF SEPTEMBER BLVD. BUSINESS',
12: 'CENTER LEGIS 4000 PLOVDIV BULGARIA',
13: 'DHABAB STREET HEAD OFFICE BUILDING 11431',
14: 'RIYADH SAUDI ARABIA',
'BIC': 2: 'AAAARSBG',
3: np.NaN,
4: 'AAACKWKW',
5: np.NaN,
6: np.NaN,
7: 'AAADFRP1',
8: np.NaN,
9: 'AAAGFRP1',
10: np.NaN,
11: 'AAAJBG21',
12: np.NaN,
13: 'AAALSARI',
14: np.NaN
df = pd.DataFrame(data=data)
python pandas dataframe
Is keeping the original index important, or can that just be a new index?
– ALollz
Mar 7 at 21:22
@ALollz No index is not important. There can be new index.
– user2958481
Mar 7 at 21:23
add a comment |
I have csv file parsed from pdf file and it is not correctly parsed as the table in pdf file has multiple rows.
importing it into pd DataFrame looks like this.
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, AAAARSBG
3 NaN REPUBLIC OF NaN
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA AAACKWKW
5 NaN OPPOSITE PUBLIC LIBRARY 13022 KUWAIT NaN
6 NaN KUWAIT NaN
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS AAADFRP1
8 NaN FRANCE NaN
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
10 NaN NaN NaN
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS AAAJBG21
12 NaN CENTER LEGIS 4000 PLOVDIV BULGARIA NaN
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 AAALSARI
14 NaN RIYADH SAUDI ARABIA NaN
I want to concatenate next rows into current row if next value in Record column is NaN.
I mean I want to get
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
here is dataframe
import numpy a np
data = 'Record': 2: '2007-03-03',
3: np.NaN,
4: '1994-03-07',
5: np.NaN,
6: np.NaN,
7: '2006-06-03',
8: np.NaN,
9: '2006-06-03',
10: np.NaN,
11: '2014-07-05',
12: np.NaN,
13: '1989-03-29',
14: np.NaN,
'Operational Address': 2: 'Omladinskih Brigada 90V 11070 BEOGRAD SERBIA,',
3: 'REPUBLIC OF',
4: 'SAFAT ALI AL SALEM STREET, MUBARAKIA',
5: 'OPPOSITE PUBLIC LIBRARY 13022 KUWAIT',
6: 'KUWAIT',
7: 'CHEZ NSM 3, AVENUE HOCHE 75008 PARIS',
8: 'FRANCE',
9: '10 RUE DU COLISEE 75008 PARIS FRANCE',
10: np.NaN,
11: '152, 6TH OF SEPTEMBER BLVD. BUSINESS',
12: 'CENTER LEGIS 4000 PLOVDIV BULGARIA',
13: 'DHABAB STREET HEAD OFFICE BUILDING 11431',
14: 'RIYADH SAUDI ARABIA',
'BIC': 2: 'AAAARSBG',
3: np.NaN,
4: 'AAACKWKW',
5: np.NaN,
6: np.NaN,
7: 'AAADFRP1',
8: np.NaN,
9: 'AAAGFRP1',
10: np.NaN,
11: 'AAAJBG21',
12: np.NaN,
13: 'AAALSARI',
14: np.NaN
df = pd.DataFrame(data=data)
python pandas dataframe
I have csv file parsed from pdf file and it is not correctly parsed as the table in pdf file has multiple rows.
importing it into pd DataFrame looks like this.
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, AAAARSBG
3 NaN REPUBLIC OF NaN
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA AAACKWKW
5 NaN OPPOSITE PUBLIC LIBRARY 13022 KUWAIT NaN
6 NaN KUWAIT NaN
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS AAADFRP1
8 NaN FRANCE NaN
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
10 NaN NaN NaN
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS AAAJBG21
12 NaN CENTER LEGIS 4000 PLOVDIV BULGARIA NaN
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 AAALSARI
14 NaN RIYADH SAUDI ARABIA NaN
I want to concatenate next rows into current row if next value in Record column is NaN.
I mean I want to get
Record Operational Address BIC
2 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
4 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
7 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
9 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
11 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
13 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
here is dataframe
import numpy a np
data = 'Record': 2: '2007-03-03',
3: np.NaN,
4: '1994-03-07',
5: np.NaN,
6: np.NaN,
7: '2006-06-03',
8: np.NaN,
9: '2006-06-03',
10: np.NaN,
11: '2014-07-05',
12: np.NaN,
13: '1989-03-29',
14: np.NaN,
'Operational Address': 2: 'Omladinskih Brigada 90V 11070 BEOGRAD SERBIA,',
3: 'REPUBLIC OF',
4: 'SAFAT ALI AL SALEM STREET, MUBARAKIA',
5: 'OPPOSITE PUBLIC LIBRARY 13022 KUWAIT',
6: 'KUWAIT',
7: 'CHEZ NSM 3, AVENUE HOCHE 75008 PARIS',
8: 'FRANCE',
9: '10 RUE DU COLISEE 75008 PARIS FRANCE',
10: np.NaN,
11: '152, 6TH OF SEPTEMBER BLVD. BUSINESS',
12: 'CENTER LEGIS 4000 PLOVDIV BULGARIA',
13: 'DHABAB STREET HEAD OFFICE BUILDING 11431',
14: 'RIYADH SAUDI ARABIA',
'BIC': 2: 'AAAARSBG',
3: np.NaN,
4: 'AAACKWKW',
5: np.NaN,
6: np.NaN,
7: 'AAADFRP1',
8: np.NaN,
9: 'AAAGFRP1',
10: np.NaN,
11: 'AAAJBG21',
12: np.NaN,
13: 'AAALSARI',
14: np.NaN
df = pd.DataFrame(data=data)
python pandas dataframe
python pandas dataframe
edited Mar 7 at 21:16
ALollz
16k31738
16k31738
asked Mar 7 at 21:02
user2958481user2958481
148111
148111
Is keeping the original index important, or can that just be a new index?
– ALollz
Mar 7 at 21:22
@ALollz No index is not important. There can be new index.
– user2958481
Mar 7 at 21:23
add a comment |
Is keeping the original index important, or can that just be a new index?
– ALollz
Mar 7 at 21:22
@ALollz No index is not important. There can be new index.
– user2958481
Mar 7 at 21:23
Is keeping the original index important, or can that just be a new index?
– ALollz
Mar 7 at 21:22
Is keeping the original index important, or can that just be a new index?
– ALollz
Mar 7 at 21:22
@ALollz No index is not important. There can be new index.
– user2958481
Mar 7 at 21:23
@ALollz No index is not important. There can be new index.
– user2958481
Mar 7 at 21:23
add a comment |
1 Answer
1
active
oldest
votes
Use cumsum to form groups and specify a dictionary for aggregation for each column.
agg_d = 'Record': 'first',
'Operational Address': lambda x: ' '.join(x.dropna()),
'BIC': 'first'
df.groupby(df.Record.notnull().cumsum().rename(None)).agg(agg_d)
Record Operational Address BIC
1 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
2 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
3 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
4 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
5 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
6 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052731%2fconcatenate-pandas-rows-if-next-row-has-nan-in-specific-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use cumsum to form groups and specify a dictionary for aggregation for each column.
agg_d = 'Record': 'first',
'Operational Address': lambda x: ' '.join(x.dropna()),
'BIC': 'first'
df.groupby(df.Record.notnull().cumsum().rename(None)).agg(agg_d)
Record Operational Address BIC
1 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
2 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
3 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
4 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
5 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
6 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
add a comment |
Use cumsum to form groups and specify a dictionary for aggregation for each column.
agg_d = 'Record': 'first',
'Operational Address': lambda x: ' '.join(x.dropna()),
'BIC': 'first'
df.groupby(df.Record.notnull().cumsum().rename(None)).agg(agg_d)
Record Operational Address BIC
1 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
2 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
3 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
4 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
5 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
6 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
add a comment |
Use cumsum to form groups and specify a dictionary for aggregation for each column.
agg_d = 'Record': 'first',
'Operational Address': lambda x: ' '.join(x.dropna()),
'BIC': 'first'
df.groupby(df.Record.notnull().cumsum().rename(None)).agg(agg_d)
Record Operational Address BIC
1 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
2 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
3 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
4 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
5 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
6 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
Use cumsum to form groups and specify a dictionary for aggregation for each column.
agg_d = 'Record': 'first',
'Operational Address': lambda x: ' '.join(x.dropna()),
'BIC': 'first'
df.groupby(df.Record.notnull().cumsum().rename(None)).agg(agg_d)
Record Operational Address BIC
1 2007-03-03 Omladinskih Brigada 90V 11070 BEOGRAD SERBIA, REPUBLIC OF AAAARSBG
2 1994-03-07 SAFAT ALI AL SALEM STREET, MUBARAKIA OPPOSITE PUBLIC LIBRARY 13022 KUWAIT KUWAIT AAACKWKW
3 2006-06-03 CHEZ NSM 3, AVENUE HOCHE 75008 PARIS FRANCE AAADFRP1
4 2006-06-03 10 RUE DU COLISEE 75008 PARIS FRANCE AAAGFRP1
5 2014-07-05 152, 6TH OF SEPTEMBER BLVD. BUSINESS CENTER LEGIS 4000 PLOVDIV BULGARIA AAAJBG21
6 1989-03-29 DHABAB STREET HEAD OFFICE BUILDING 11431 RIYADH SAUDI ARABIA AAALSARI
answered Mar 7 at 21:28
ALollzALollz
16k31738
16k31738
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55052731%2fconcatenate-pandas-rows-if-next-row-has-nan-in-specific-column%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is keeping the original index important, or can that just be a new index?
– ALollz
Mar 7 at 21:22
@ALollz No index is not important. There can be new index.
– user2958481
Mar 7 at 21:23