Add new columns to DataFrame from the value of one column

hunter

I have a pandas DataFrame, there is a column with values like a,b,c i.e. string splited by ','. Now I want to create new columns, for example, for a,b,c there would be new column a, column b, column c. then the data with a,b,c would get a value of true on the three columns, the data with a,b,e would get true on columns a and b but false on c, maybe it is more clearly to see the picture below. How to do this?

enter image description here

jezrael

Use str.get_dummies with cast to bool by astype and add column B by join:

df1 = df['A'].str.get_dummies(',').astype(bool).join(df['B'])
print (df1)
       a     b     c      f  B
0   True  True  True  False  3
1  False  True  True   True  4

More general solution with pop for extract column A:

df = pd.DataFrame({'A':['a,b,c','b,c,f'], 'B':[3,4], 'C':[7,3]})
print (df)
       A  B  C
0  a,b,c  3  7
1  b,c,f  4  3

df1 = df.pop('A').str.get_dummies(',').astype(bool).join(df)
print (df1)
       a     b     c      f  B  C
0   True  True  True  False  3  7
1  False  True  True   True  4  3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spark add new column to dataframe with value from previous row

Mapping columns from one dataframe to another to create a new column

Create new dataframe columns from one column with different values and types

pandas: add new column with value from either of two other columns

Comparing a value from one dataframe with values from columns in another dataframe and getting the data from third column

Python: Set new column value if multiple columns in a dataframe have any value other than one specific string

How to add strings of one columns of the dataframe and form another column that will have the incremental value of the original column

Find nearest value from multiple columns and add to a new column in Python

Search for value in all DataFrame columns (except first column !) and add new column with matching column name

Add a new column to a dataframe based on results from other columns

Pandas: add a new column with one single value at the last row of a dataframe

Add new column with one value

Add new column in pandas dataframe using empty string or the value from column A depending on the value on column B

Creating new new data frame with so many columns from existing dataframe by dropping one column

Create new dataframe with tuple of cartesian product of few columns as one column and keep remaining columns from the same row

How to add new columns to dataframe with value taken from another dataframe?

How to add a column of random numbers to a dataframe by each value in one of the columns?

How to copy all column values of a dataframe into new columns of another one according to the index of the first and a column value of the second

Add a new column to a dataframe based on multiple columns from another dataframe

Pandas: Add new column and assigning value from another dataframe by condition

How to add a new pandas column whose value is conditioned on one column, but value depends on other columns?

Split one column of dataframe to new columns in pandas

how to add data from one dataframe to a new column on another dataframe

Add a new column to a Pandas dataframe with a value from a function

Make new dataframe from existing dataframe with unique values from one column and corresponding values from other columns

Iterate over pairs of columns and add value from one under condition in new column Python

Check if column name from one dataframe matches with index value of another dataframe and populate value into a new column

Add column in DataFrame with first non null value from select columns

How to extract value from a spark dataframe and add it to a second one as a column?

TOP Ranking

HotTag

Archive