nonoumasy
nonoumasy

Reputation: 11

How to concatenate all rows into one row of a multi-column DataFrame?

In Python,

  1. How best to combine all rows of each column in a multi-column DataFrame 
into one column,
  2. separated by ‘ | ’ separator
  3. including null values

    import pandas as pd html = 'https://en.wikipedia.org/wiki/Visa_requirements_for_Norwegian_citizens' df = pd.read_html(html, header=0) df= df[1] df.to_csv('norway.csv)

From This: To This:

Upvotes: 1

Views: 1378

Answers (2)

jezrael
jezrael

Reputation: 862641

I believe you need replace missing values if necessary by fillna, convert values to strings with astype and apply with join. Get Series, so for one column DataFrame add to_frame with transposing:

df = df.fillna(' ').astype(str).apply('|'.join).to_frame().T
print (df)
                      Country Allowed_stay        Visa_requirement
0  Albania|Afganistan|Andorra     30|30|60  visa free| | visa free

Or use list comprehension with DataFrame constructor:

L = ['|'.join(df[x].fillna(' ').astype(str)) for x in df]
df1 = pd.DataFrame([L], columns=df.columns)
print (df1)
                      Country Allowed_stay        Visa_requirement
0  Albania|Afganistan|Andorra     30|30|60  visa free| | visa free

Upvotes: 1

caverac
caverac

Reputation: 1637

df = pandas.DataFrame([
    {'A' : 'x', 'B' : 2, 'C' : None},
    {'A' : None, 'B' : 2, 'C' : 1},
    {'A' : 'y', 'B' : None, 'C' : None},
])

pandas.DataFrame(df.fillna('').apply(lambda x: '|'.join(x.astype(str)), axis = 0)).transpose()

Upvotes: 1

Related Questions