How to Break a List into Columns

Is there a way to break a list into columns?

The CSS solution is: http://www.w3.org/TR/css3-multicol/

The browser support is exactly what you'd expect..

It works "everywhere" except Internet Explorer 9 or older: http://caniuse.com/multicolumn

ul {
-moz-column-count: 4;
-moz-column-gap: 20px;
-webkit-column-count: 4;
-webkit-column-gap: 20px;
column-count: 4;
column-gap: 20px;
}

See: http://jsfiddle.net/pdExf/

If IE support is required, you'll have to use JavaScript, for example:

http://welcome.totheinter.net/columnizer-jquery-plugin/

Another solution is to fallback to normal float: left for only IE. The order will be wrong, but at least it will look similar:

See: http://jsfiddle.net/NJ4Hw/

<!--[if lt IE 10]>
<style>
li {
width: 25%;
float: left
}
</style>
<![endif]-->

You could apply that fallback with Modernizr if you're already using it.

Split a Pandas column of lists into multiple columns

You can use the DataFrame constructor with lists created by to_list:

import pandas as pd

d1 = {'teams': [['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG'],
['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG'],['SF', 'NYG']]}
df2 = pd.DataFrame(d1)
print (df2)
teams
0 [SF, NYG]
1 [SF, NYG]
2 [SF, NYG]
3 [SF, NYG]
4 [SF, NYG]
5 [SF, NYG]
6 [SF, NYG]


df2[['team1','team2']] = pd.DataFrame(df2.teams.tolist(), index= df2.index)
print (df2)
teams team1 team2
0 [SF, NYG] SF NYG
1 [SF, NYG] SF NYG
2 [SF, NYG] SF NYG
3 [SF, NYG] SF NYG
4 [SF, NYG] SF NYG
5 [SF, NYG] SF NYG
6 [SF, NYG] SF NYG

And for a new DataFrame:

df3 = pd.DataFrame(df2['teams'].to_list(), columns=['team1','team2'])
print (df3)
team1 team2
0 SF NYG
1 SF NYG
2 SF NYG
3 SF NYG
4 SF NYG
5 SF NYG
6 SF NYG

A solution with apply(pd.Series) is very slow:

#7k rows
df2 = pd.concat([df2]*1000).reset_index(drop=True)

In [121]: %timeit df2['teams'].apply(pd.Series)
1.79 s ± 52.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [122]: %timeit pd.DataFrame(df2['teams'].to_list(), columns=['team1','team2'])
1.63 ms ± 54.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

Split lists into multiple columns in a pandas DataFrame

What you could do is instead of appending columns on every iteration append all of them after running your loop:

df3 = pd.DataFrame(columns=['name', 'hobby'])
d_list = []

for index, row in df.iterrows():
for value in str(row['hobbies']).split(';'):
d_list.append({'name':row['name'],
'value':value})
df3 = df3.append(d_list, ignore_index=True)
df3 = df3.groupby('name')['value'].value_counts()
df3 = df3.unstack(level=-1).fillna(0)
df3

I checked how much time it would take for you example dataframe. With the improvement I suggest it's ~50 times faster.

split list values into columns :python

Not sure it's the best option, but I did it using regex:
(Assuming your last column always looks like '[index 12 Score 0.14788]')

import pandas as pd
import re

dfData = {
'col_x' : ['msg1', 'msg2'],
'col_y' : ['[index 12 Score 0.14788]', '[index 4 Score 0.002986]'],
}

df = pd.DataFrame(data=dfData)

indexes = []
scores = []

for i in df.col_y:
infos = re.search('\[index (.+) Score (.+)\]', i)
indexes.append(infos.group(1))
scores.append(infos.group(2))

df['index'] = indexes
df['score'] = scores
df.drop('col_y', axis = 1, inplace = True)
df.head()

is there a way to break list into columns using VueJs?

You can use CSS to easily create columns. Just use a v-for to get the content on the screen then style it with CSS. In your case the loop will append multiple <div> elements. Here's an example of CSS columns.

This is the most basic solution where column-count: 3 splits the list into 3 equal columns.

.column_wrapper {  column-count: 3;}
<div class="column_wrapper">  <!-- results of <div v-for="item in list"></div>  -->  <div>Item 1</div>  <div>Item 2</div>  <div>Item 3</div>  <div>Item 4</div>  <div>Item 5</div>  <div>Item 6</div>  <div>Item 7</div>  <div>Item 8</div>  <div>Item 9</div>  <div>Item 10</div>  <div>Item 11</div>  <div>Item 12</div>  <div>Item 13</div>  <div>Item 14</div>  <div>Item 15</div></div>

Splitting an Ordered List into two columns

As said in the comments, you can remove the margin-top: 1rem; from ol.gradient-list>li (open snippet in full-page mode to see it properly).

.ss {
background-color: rgba(67, 55, 76, 0.99);
background-image: linear-gradient(to bottom right, #f54171, #29c450de 2100px);
position: relative;
}

.ss::before {
position: absolute;
z-index: 100;
top: 0;
left: 0;
display: block;
width: 100%;
height: 500px;
content: " ";
background-image: -webkit-gradient(linear, left bottom, left top, from(rgba(0, 0, 0, 0)), to(rgba(0, 0, 0, 0.1)));
background-image: linear-gradient(to top, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.1));
}

.ss::after {
position: absolute;
z-index: 50;
bottom: 0;
left: 0;
display: block;
width: 100%;
height: 1077px;
content: " ";
background-image: url("../img/section_pattern01.png");
opacity: 1;
background-position: -80px 50px;
}

.ss_hollow-arrow {
width: 100%;
height: 40px;
background-image: url("../img/content_hollow-arrow.png");
background-position: 50% 50%;
position: relative;
top: -40px;
left: 0;
opacity: 1;
}

.ss_wrapper {
position: relative;
z-index: 200;
width: 100%;
max-width: 1200px;
margin: 0 auto;
padding-top: 110px;
padding-bottom: 200px;
}

ol.gradient-list>li,
ol.gradient-list>li::before {
box-shadow: 0.25rem 0.25rem 0.6rem rgba(0, 0, 0, 0.05), 0 0.5rem 1.125rem rgba(75, 0, 0, 0.05);
}

ol {
columns: 2;
-webkit-columns: 2;
-moz-columns: 2;
display: flexbox;
}

ol.gradient-list {
counter-reset: gradient-counter;
list-style: none;
margin: 1.75rem 0;
padding-left: 25%;
font-weight: bold;
font-size: 25px;
padding-top: 8%;
width: fit-content;
align-content: center;
}

ol.gradient-list>li {
background: white;
border-radius: 0 0.5rem 0.5rem 0.5rem;
counter-increment: gradient-counter;
min-height: 3rem;
padding: 1rem 1rem 1rem 3rem;
position: relative;
text-align: center;
-webkit-column-break-inside: avoid;
page-break-inside: avoid;
break-inside: avoid;
}

ol.gradient-list>li::before {
align-items: center;
content: counter(gradient-counter);
color: #1d1f20;
display: flex;
font-size: 25px;
justify-content: flex-end;
padding: 0.125em 0.25em;
z-index: 1;
font-weight: bold;
}

ol.gradient-list>li:nth-child(10n+1):before {
background: linear-gradient(135deg, rgba(7, 207, 233, 0.534) 0%, rgba(221, 7, 96, 0.726) 100%);
}

ol.gradient-list>li:nth-child(10n+2):before {
background: linear-gradient(135deg, rgba(162, 237, 86, 0.4) 0%, rgba(253, 220, 50, 0.4) 100%);
}

ol.gradient-list>li:nth-child(10n+3):before {
background: linear-gradient(135deg, rgba(162, 237, 86, 0.6) 0%, rgba(253, 220, 50, 0.6) 100%);
}

ol.gradient-list>li:nth-child(10n+4):before {
background: linear-gradient(135deg, rgba(162, 237, 86, 0.8) 0%, rgba(28, 128, 209, 0.8) 100%);
}

ol.gradient-list>li:nth-child(10n+5):before {
background: linear-gradient(135deg, rgb(56, 95, 201) 0%, rgb(206, 75, 108) 100%);
}

ol.gradient-list>li:nth-child(10n+6):before {
background: linear-gradient(135deg, rgba(192, 45, 101, 0.8) 0%, rgba(27, 167, 155, 0.8) 100%);
}

ol.gradient-list>li:nth-child(10n+7):before {
background: linear-gradient(135deg, rgba(162, 237, 86, 0.6) 0%, rgba(253, 220, 50, 0.6) 100%);
}

ol.gradient-list>li:nth-child(10n+8):before {
background: linear-gradient(135deg, rgba(162, 237, 86, 0.4) 0%, rgba(253, 220, 50, 0.4) 100%);
}

ol.gradient-list>li:nth-child(10n+9):before {
background: linear-gradient(135deg, rgba(162, 237, 86, 0.2) 0%, rgba(253, 220, 50, 0.2) 100%);
}

ol.gradient-list>li:nth-child(10n+10):before {
background: linear-gradient(135deg, rgba(162, 237, 86, 0) 0%, rgba(253, 220, 50, 0) 100%);
}

ol.gradient-list>li+li {
margin-top: 2rem;
font-weight: bold;
font-size: 25px;
}

ol.listitem {
text-align: center;
}
<section id="softskill" class="ss">

<div class="ss_wrapper">
<div class="section-header">
<div class="section-header__title section-header__title--softskill">My Soft Skills...</div>
<div class="section-header__subtitle">Software and technologies that I'm experienced in</div>
</div>

<ol class="gradient-list">

<li>Communication Skills</li>
<li>Time Management</li>
<li>Critical Thinking</li>
<li>Creative Thinking</li>
<li>Leadership Skills</li>
<li>Disciplined</li>
<li>Positive Attitude</li>
<li>Confidence</li>
<li>Problem Solving</li>
<li>Active Listening</li>

</ol>

<table>

</table>
</div>
</section>

Splitting a list in a Pandas cell into multiple columns

You can loop through the Series with apply() function and convert each list to a Series, this automatically expand the list as a series in the column direction:

df[0].apply(pd.Series)

# 0 1 2
#0 8 10 12
#1 7 9 11

Update: To keep other columns of the data frame, you can concatenate the result with the columns you want to keep:

pd.concat([df[0].apply(pd.Series), df[1]], axis = 1)

# 0 1 2 1
#0 8 10 12 A
#1 7 9 11 B


Related Topics



Leave a reply



Submit