Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am working on a data frame, trying to convert the columns to the list. It is concatenating all the data which are present in the column. But I didn't want to convert those columns which contain NA values.

data = {'Name':['Tom', 'nick'], 'Grade':[[20, 21],[ 19, 18]],'Science':['23',None]} 

df=pd.DataFrame(data)

df['Grade']=df['Grade'].append(df['Science'],ignore_index=True)

df.apply(lambda row: row['Grade'].append(row['Science']), axis=1)

Output:

    Name    Grade   Science

0   Tom     [20, 21, 23]    23

1   nick    [19, 18, None]  None

But I wanted the results to be:

    Name    Grade   Science

0   Tom     [20, 21, 23]    23

1   nick    [19, 18]    None

1 Answer

0 votes
by (36.8k points)

Group the 'Species' using the group_by method and then sum the 'Weight' column and then plot the graph.

library(dplyr)

df %>%

      group_by(Species) %>%

      summarise(Weight = log(sum(Weight))) %>% 

      ggplot(aes(x = Species, y = Weight)) + 

              geom_col()

Or You can use base R

aggregate(Weight ~ Species, df, sum)

Then use the bar plot if needed.

barplot(rowsum(df$Weight, df$Species)[,1])

If you want to use the log then you can wrap with log, check out the code to do it:

barplot(log(rowsum(df$Weight, df$Species))[,1])

Data i have used is:

df  <- structure(list(Species = c("Dog", "Cat", "Dog", "Dog", "Cat", 

"Dog", "Cat"), Weight = c(7L, 2L, 5L, 4L, 3L, 9L, 2L)), class = "data.frame", row.names = c("1", 

"2", "3", "4", "245", "246", "247"))

 If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Browse Categories

...