Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas FutureWarning: Columnar iteration over characters will be deprecated in future releases

I have an existing solution to split a dataframe with one column into 2 columns.

df['A'], df['B'] = df['AB'].str.split(' ', 1).str

Recently, I got the following warning FutureWarning: Columnar iteration over characters will be deprecated in future releases.

How to fix this warning?

I'm using python 3.7

like image 888
user3848207 Avatar asked Apr 20 '20 00:04

user3848207


2 Answers

That's not entirely correct, plus the trailing .str does not make sense. Since split with expand returns a DataFrame, this is easier:

df[['A', 'B']] = df['AB'].str.split(' ', n=1, expand=True)

Your existing method without expand returns a single Series with a list of columns. I'm not sure what version of pandas used to work with your code but AFAIK you'll need to make some tweaks for this to work with pandas (>= 1.0) today. Assignment in this way is tedious but still possible.

s = df['AB'].str.split(' ', n=1)
df['A'], df['B'] = s.str[0], s.str[1]

I prefer the expand solution as it's a line shorter.

like image 126
cs95 Avatar answered Oct 06 '22 16:10

cs95


Or we do

df['A'], df['B']=zip(*df['AB'].str.split(' ').tolist())
df
    AB  A  B
0  A B  A  B
1  A B  A  B
2  A B  A  B
3  A B  A  B
like image 3
BENY Avatar answered Oct 06 '22 15:10

BENY