Split one column to two columns depending one the content in pandas dataframe

freefrog

I have a pandas DataFrame like this:

df = pd.DataFrame(['A',1,2,3,'B',4,5,'C',6,7,8,9])

    0
0   A
1   1
2   2
3   3
4   B
5   4
6   5
7   C
8   6
9   7
10  8
11  9

It's mix of strings and numbers. I want to split this DF into tow columns like this:

   name value
0   A   1
1   A   2
2   A   3
3   B   4
4   B   5
5   C   6
6   C   7
7   C   8
8   C   9

what's an efficient way to do this?

jezrael

You can use:

df = pd.DataFrame({0 :['A',1,2,3,'B',4,5,'C',6,7,8,9]})
#check strings 
mask = df[0].astype(str).str.isalpha()
#check if mixed values - numeric with strings 
#mask = df[0].apply(lambda x: isinstance(x, str))
#create column to first position, create NaNs filled by forward filling
df.insert(0, 'name', df[0].where(mask).ffill())
#remove rows with same values - with names, rename column
df = df[df['name'] != df[0]].rename(columns={0:'value'}).reset_index(drop=True)
print (df)
  name value
0    A     1
1    A     2
2    A     3
3    B     4
4    B     5
5    C     6
6    C     7
7    C     8
8    C     9

Or:

out = []
acc = None
for x in df[0]:
    #check if strings
    if isinstance(x, str):
        #assign to variable for tuples
        acc = x
    else:
        #append tuple to out
        out.append((acc, x))
print (out)

df = pd.DataFrame(out, columns=['name','value'])
print (df)
  name  value
0    A      1
1    A      2
2    A      3
3    B      4
4    B      5
5    C      6
6    C      7
7    C      8
8    C      9

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Split one column of dataframe to new columns in pandas

SQL Server : update two columns with the split content of one existing column

how to split one spark dataframe column into two columns by conditional when

filter pandas dataframe by two columns where one column is a list

How to shift and stack two columns of pandas Dataframe into one column?

Sum of every two columns and leave one column in pandas dataframe

How to split one dataframe column into many columns

R - Split a one column dataframe into multiple columns

Split one column into two columns and retaining the seperator

How can I split a DataFrame column with datetimes into two columns: one with dates and one with times of the day?

split one pandas column text to multiple columns

Split One Column to Multiple Columns in Pandas

Python Pandas : compare the rows of two csv(dataframe) for similar values along one column and return content of rows (columns) with similarites

Pandas Dataframe: How to split one column into multiple one-hot-encoded columns

SQL : Split one row into two rows depending on column

Pandas DataFrame efficiently split one column into multiple

Split with pandas dataframe column duplicate values into two dataframes, one with duplicate, one without duplicate

PYTHON DATAFRAME - SPLIT ONE COLUMN of numbers [0,0] DATAFRAME into TWO COLUMNS

How do I split data out from one column of a pandas dataframe into multiple columns of a new dataframe

How to melt two dataframe columns into one column

How to merge the two columns from two dataframe into one column of a new dataframe (pandas)?

How to join two columns (one is list other is integer id column ) in pandas python in one dataframe?

How do I convert a Pandas Dataframe with one column into a Pandas Dataframe of two columns?

How to merge two columns into one in pandas dataframe

Replace all columns in pandas dataframe with one column

compare multiple columns of pandas dataframe with one column

Pandas, DataFrame: Splitting one column into multiple columns

Change the column of the dataframe Pandas to value of one of the columns

pandas map one column to the combination of two columns