简体   繁体   中英

Pandas: Add dataframes columns names to rows after join procedure

I have the following dataframe:

df1 = pd.DataFrame({'ID'    : ['T1002.', 'T5006.', 'T5007.'],
                    'Parent': ['Stay home.', "Stay home.","Stay home."],
                    'Child' : ['2Severe weather.', "5847.", "Severe weather."]})



      ID    Parent       Child
0   T1002.  Stay home.  2Severe weather.
1   T5006.  Stay home.  5847.
2   T5007.  Stay home.  Severe weather.

I want to add the two columns into one and also add the columns' names into the rows. I want also the columns' names to be in bold.

Expected outcome: (I cannot make bold the columns names ID, etc)

             Joined_columns()
0   ID: T1002.  Parent: Stay home.   Child: 2Severe weather.
1   ID: T5006.  Parent: Stay home.   Child: 5847.
2   ID: T5007.  Parent: Stay home.   Child: Severe weather.

The join is accomplished with the following code:

df1_final=df1.stack().groupby(level=0).apply(' '.join).to_frame(0)

But I am not sure how to go to the end. Any ideas?

I want to add the two columns into one and also add the columns' name into the rows. I want also the columns names to be in bold.

i would like to have the bold text on Excel.

applying formats to substrings within cells in an excel worksheet requires writing a rich string

It is necessary to construct a list of rich strings and then write them out to the worksheet iteratively. The difficulty with this approach is that you have to keep track of the index offset with respect to the rest of the data frame.

import pandas as pd
import xlsxwriter

# create a workbook / worksheet and format
book = xlsxwriter.Workbook('text.xlsx')
sheet = book.add_worksheet('joined-columns')
bold = book.add_format({'bold': True})

# generate the formatted rich string
df1 = pd.DataFrame({'ID': ['T1002.', 'T5006.', 'T5007.'],
                    'Parent': ['Stay home.', "Stay home.","Stay home."],
                    'Child': ['2Severe weather.', "5847.", "Severe weather."]})
# the list of string fragments is generated using 
# a double list comprehension.
# this flattens the list of lists, where 
# each inner list is [bold, 'key: ', 'value', ' '].
# the trailing space is removed through `[:-1]`
data = df1.apply(
  lambda row: [a 
               for k, v in row.to_dict().items() 
               for a in [bold, f'{k}: ', v, ' ']
              ][:-1], 
  axis=1
).to_list()


# write the title & rich strings
# the list of string fragments has to be unpacked with `*`
# because `write_rich_string` accepts variable
# number or arguments after the cell is specified.
sheet.write(0, 0, 'Joined_columns()')
for idx, cell in enumerate(data):
    sheet.write_rich_string(idx + 1, 0, *cell)

book.close()

它在电子表格中的外观

To get the column names, you can use .name :

>>> df1.apply(lambda sr: f'{sr.name}: ' + sr).apply(' '.join, axis=1).to_frame('Joined_columns()')

Output:

                                        Joined_columns()
0  ID: T1002. Parent: Stay home. Child: 2Severe weather.
1             ID: T5006. Parent: Stay home. Child: 5847.
2   ID: T5007. Parent: Stay home. Child: Severe weather.

To make the columns bold depends on what you're using to display them. For example, ** is often used to signify bold in markdown languages and <b>...</b> tags are used in HTML.

>>> print(df1.apply(lambda sr: f'**{sr.name}**: ' + sr).apply(' '.join, axis=1).to_string(index=False))

Output:

ID : T1002. Parent : Stay home. Child : 2Severe weather.

ID : T5006. Parent : Stay home. Child : 5847.

ID : T5007. Parent : Stay home. Child : Severe weather.

or

>>> print(df1.apply(lambda sr: f'<b>{sr.name}</b>: ' + sr).apply(' '.join, axis=1).to_string(index=False))

Output:

: T1002. : Stay home. : 2Severe weather.
: T5006. : Stay home. : 5847.
: T5007. : Stay home. : Severe weather.

To get a name of a column in front of its values all you need is:

df1.columns + ": " + df1

In case if types of df1 values may not be str :

df1.columns + ": " + df1.astype(str)

To glue values along the records together:

(df1.columns + ": " + df1).agg(" ".join, axis=1)

Formatting depends on the output media. If it's an HTML document we use tags or CSS. In jupyter we access html-formatting features with Styler to format cells. But in this case you have to format partially the text in a cell. So it looks like we have to work with html directly:

from IPython.display import display_html

name_delim = ': '    # a string between a column name and its values
sep = ' '            # a string between adjacent columns
data = (df1.columns + name_delim + df1.astype(str)).agg(sep.join, axis=1)

# Now we transform obtained Series to DataFrame 
# in order to apply left alignment
# and get the HTML representation of the table
df_html = (
    data      
    .to_frame("Joined_columns")     
    .style.set_properties(**{'text-align': 'left'})
    .to_html()
)

# The next part may do more transformations than necessary
# in case if the data contains parts equal to some column name + ": "
# so it should be used with caution
for col in df1.columns:
    df_html = df_html.replace(col+name_delim, f'<b>{col}</b>{name_delim}')
    
display_html(df_html, raw=True)

Here's an output I get with this code:

输出

Try this:

col_= df.columns[0]
col_list = list(df.columns)
col_list.remove(col_)
[ col_ := col_+'_'+ col for col in col_list ]

df = pd.DataFrame(df.stack(level=0))
df.reset_index(inplace=True)
df[col_] = df['level_1']+ ' : ' + df[0].astype(str) + ' '
df = df[['level_0',col_]]
df = df.groupby('level_0').sum()
df.reset_index(inplace=True,drop=True)

Try:

df1['Join_columns'] = ((df1.columns + ": ") + df1).agg(' '.join, axis=1)

Output:

       PARENT            CHILD                               Join_columns
0  Stay home.  Severe weather.  PARENT: Stay home. CHILD: Severe weather.
1  Stay home.  Severe weather.  PARENT: Stay home. CHILD: Severe weather.
2  Stay home.  Severe weather.  PARENT: Stay home. CHILD: Severe weather.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM