Question

# python How to convert a dataframe column into sparse matrix? For example there is one...

# python

How to convert a dataframe column into sparse matrix?

For example there is one column [1,2,3]

I want to convert it into:

1 0 0

0 1 0

0 0 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Done accordingly. Please comment for further help. Please uprate.

Code:

import pandas as pd
from scipy import sparse
# here column is the column for which we want sparse matrix
df = pd.DataFrame({'tag1': ['sean', 'udi', 'bogdan'], 'tag2': ['a', 'b', 'c'], 'column': [1,2,3]})
# tag1 -> rows, tag2 -> columns that will help for creating sparse matrix.
df.set_index(['tag1', 'tag2'], inplace=True)

mat = sparse.coo_matrix((df.column, (df.index.codes[0], df.index.codes[1]))) #here you can use ([0,1,2],[2,1,0]) as well.
print(mat.todense())

Code Snapshot:

Output:

Add a comment
Know the answer?
Add Answer to:
# python How to convert a dataframe column into sparse matrix? For example there is one...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #Python I have a dataframe: df = pd.DataFrame({'A':[0,0,15,0,0,12,0,0,0,5]}) And I want to replace the 0 value...

    #Python I have a dataframe: df = pd.DataFrame({'A':[0,0,15,0,0,12,0,0,0,5]}) And I want to replace the 0 value with the nearest non zero value, For example, the first value is 0, then I find the the nearest non-zero value is 15, so I replace it as 15, then the data becomes:[15,0,15,0,0,12,0,0,0,5], Then for all the value except first one, I need to find the both side of the nearest non-zero value, and average them. So for the second 0, it would be...

  • I have various sums that I've calculated from my dataframe and I want to use matplotlib...

    I have various sums that I've calculated from my dataframe and I want to use matplotlib and make pi chart of percentage of students with a particular nationality. for ex, totalStudents=(df.sum()) studentsSwiss=(df1.sum()) studentsIndian=(df2.sum()) studentsChinese=(df3.sum()) etc. And I want the pi chart to be in percentage. so take in those values convert to percentage of total students of that nationality. I'm using python. additional info: df is a dataframe that has a column that sums total Swiss, Indian, Chinese nationalities and...

  • Pandas DataFrame in Python : I have csv file which has date column with object data...

    Pandas DataFrame in Python : I have csv file which has date column with object data type which ranges from 1908 to 2018: (Original) Date                 (My result) Date                  (I Need) Date                       17-Sep-08                                  2008-09-17                 1908-09-17 7-Sep-09                                    2009-09-07                  1909-09-07 .    (more years)                         .   (more years)               .     .                                                 .                                      . 8-Nov-07                                       2007-11-07                 2007-11-07 23-Sep-08                                     2008-09-23                 2008-09-23 29-Dec-18                                     2018-12-29                 2018-12-29 When I am converting it to datetime64[ns] or/and adding column as year after extracting just year values from date...

  • 6.18.1 Consider the sparse matrix X below and write C code that would store this code...

    6.18.1 Consider the sparse matrix X below and write C code that would store this code in Yale Sparse Matrix Format. Row 1 [1, 2, 0, 0, 0, 0] Row 2 [0, 0, 1, 1, 0, 0] Row 3 [0, 0, 0, 0, 9, 0] Row 4 [2, 0, 0, 0, 0, 2] Row 5 [0, 0, 3, 3, 0, 7] Row 6 [1, 3, 0, 0, 0, 1] 6.18.2 In terms of storage space, assuming that each element in...

  • (a) Load the data file data/tips.csv into a pandas DataFrame called tips_df using the pandas read_table()...

    (a) Load the data file data/tips.csv into a pandas DataFrame called tips_df using the pandas read_table() function. Check the first five rows. (b) Create a new dataframe called tips by randomly sampling 6 records from the dataframe tips_df. Refer to the sample() function documentation. (c) Add a new column to tips called idx as a list ['one', 'two', 'three', 'four', 'five', 'six'] and then later assign it as the index of tips dataframe. Display the dataframe. (d) Create a new...

  • In python how would you write this to a file for example here the example output...

    In python how would you write this to a file for example here the example output to be written to a file as and i want to use a LIST 0 < 20 20 < 40 40 < 60 60 < 80

  • Python Code to load a CSV format dataset and convert last column data to integer values...

    Python Code to load a CSV format dataset and convert last column data to integer values 0&1. Data Set URL- https://archive.ics.uci.edu/ml/machine-learning-databases/arrhythmia/

  • Write a function in python that will take in a df (dataframe) and n (integer that...

    Write a function in python that will take in a df (dataframe) and n (integer that determines the number of rows that the returned df will display): create a new dataframe df1 by copying the input df, with only two columns "fruit" and "price" from the original df. Then add two new columns to df1 labeled "brand" and "time of purchase" and assign values appropriately. Return df1 NOTE: If n= -1, return all the rows. If n ≥ 0 return...

  • Python with Pandas dataframe I have a csv file that contains a large number of columns...

    Python with Pandas dataframe I have a csv file that contains a large number of columns and rows. I need to write a script that concatenates some elements of the first row with some elements of the 2 row. Something like # if data[1][0] starts with ch then concatenate the element right below it. I have attached a picture of just a sample of my data. The booleans have to stay on there as is. But I must drop the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT