Emaa
Emaa

Reputation: 1

Split Dataframe from back to front

Somebody know how to make a split from back to front, when I make a split like

dfgeo['geo'].str.split(',',expand=True)

I have:

1,2,3,4,nan,nan,nan

but I want

nan,nan,nan,4,3,2,1

thanks peopleee :)

Upvotes: 0

Views: 165

Answers (3)

jezrael
jezrael

Reputation: 863291

Use iloc with ::-1 for swap order of columns:

dfgeo = pd.DataFrame({'geo': ['1,2,3,4', '1,2,3,4,5,6,7']})
print (dfgeo)
             geo
0        1,2,3,4
1  1,2,3,4,5,6,7

df = dfgeo['geo'].str.split(',',expand=True).iloc[:, ::-1]
#if necessary set default columns names
df.columns = np.arange(len(df.columns))
print (df)

      0     1     2  3  4  5  6
0  None  None  None  4  3  2  1
1     7     6     5  4  3  2  1

Upvotes: 0

It_is_Chris
It_is_Chris

Reputation: 14113

if you're looking to reverse the column order you can do this:

new_df = dfgeo['geo'].str.split(',', expand=True)
new_df[new_df.columns[::-1]]

Upvotes: 1

Ala Tarighati
Ala Tarighati

Reputation: 3817

Try this:

list(reversed(dfgeo['geo'].str.split(',',expand=True)))

Assuming your code returns a list!

Upvotes: 0

Related Questions