Create a table using data content as columns in python

create new dataframe from existing dataframe based on condition
create table in python pandas
create column in python
python simple table
create new dataframe from existing dataframe pandas
pandas dataframe
create dynamic dataframe in python
pandas create dataframe

I currently have raw data like this:

person1  person2   person3...
  blue     red      green
  red      blue     yellow
  black    black
  white    green
  orange

with lots of different values and columns.

What I need is:

         Blue  Red  Black  Green Yellow Orange White
Person1   Y     Y     Y                    Y     Y
Person2   Y     Y     Y      Y
Person3                      Y      Y

Any suggestions please?

Thanks

I will use get_dummies (you can add map({True:'Y',False:''}) at the end )

s=pd.get_dummies(df1)
s.columns=pd.MultiIndex.from_tuples(s.columns.str.split('_').map(tuple))
Yourdf=s.stack(0).sum(level=1).eq(1)
Yourdf
Out[132]: 
         black   blue  green  orange    red  white  yellow
person1   True   True  False    True   True   True   False
person2   True   True   True   False   True  False   False
person3  False  False   True   False  False  False    True

Or

pd.concat([df1[x].str.get_dummies() for x in df1.columns],keys=df1.columns,axis=1).\
        stack(1).sum(level=1).T.eq(1)
Out[164]: 
         black   blue  green  orange    red  white  yellow
person1   True   True  False    True   True   True   False
person2   True   True   True   False   True  False   False
person3  False  False   True   False  False  False    True

Creating Pandas DataFrames & Selecting Data, Create a pandas DataFrame with data; Select columns in a DataFrame; Select A DataFrame is a table much like in SQL or Excel. Visit some of the URLs you see in this dataset to familiarize yourself with the structure of the site and content, � Note: For more information, refer to Python GUI – tkinter. Creating Tables Using Tkinter. A table is useful to display data in the form of rows and columns. Unfortunately, Tkinter does not provide a Table widget to create a table. But we can create a table using alternate methods.

Method 1: DataFrame.apply + pd.value_counts

new_df=df.apply(pd.value_counts).replace({1:'Y',np.nan:''}).T
print(new_df)

        black blue green orange red white yellow
person1     Y    Y            Y   Y     Y       
person2     Y    Y     Y          Y             
person3                Y                       Y 

Method 2: pd.crosstab + DataFrame.melt

df2=df.melt()
new_df=pd.crosstab(df2['variable'],df2['value']).replace({0:'',1:'Y'}).rename_axis(index=None,columns=None)
print(new_df)
        black blue green orange red white yellow
person1     Y    Y            Y   Y     Y       
person2     Y    Y     Y          Y             
person3                Y                       Y

How to create new columns derived from existing columns?, The latter was already used in the subset data tutorial to filter rows of a table using a conditional expression. I want to rename the data columns to the� load all of the data into the table up-front. If your data is large, then this can be prohibitively slow. In this chapter, you’ll learn how to write your own filtering, sorting, and paging backends in Python with Dash. We’ll do the data processing with Pandas but you could write your own routines with SQL or even generate the data on the fly!

I have a primitive approach using dictionary data type and print function,

columns = ("Blue", "Red", "Black", "Green", "Yellow", "Orange", "White")

table_dict = {"Person1": ("Y", "Y", "Y", " ", " ",  "Y", "Y"),
          "Person2": ("Y", "Y", "Y", "Y ", " ", " ", " "),
          "Person3": (" ", " ", " ", "Y", "Y", " ", " ")}

print(" "*5, *columns, sep=" "*5)

for person in table_dict:
    print(person, end=" "*4)
    print(*table_dict.get(person), sep=" "*9)

Output:

          Blue     Red     Black     Green     Yellow     Orange     White
Person1    Y         Y         Y                             Y         Y
Person2    Y         Y         Y         Y                               
Person3                                  Y         Y                    

Constructing a table — Astropy v4.0.1, These might be Python lists or numpy arrays or a mix of the two. These can be used to create a Table by putting the column data variables into a Python list. In this� In order to do so, you’ll need to add the following 3 components into the code: import matplotlib.pyplot as plt at the top of the code. plot () at the end of the ‘pivot’ variable. plt.show () at the bottom of the code. Before you can run the code below, make sure that the matplotlib package is installed in Python.

This is a working implementation, let me know what you think:

d1={'person1': ['blue', 'red', 'black', 'white', 'orange'], 'person2': ['red', 'blue', 'black', 'green', ''], 'person3': ['green', 'yellow', '', '', '']}
df1 = pd.DataFrame(data=d1)
new_df1 = df1.apply(pd.value_counts).replace({1:'Y',np.nan:''})
new_df1 = new_df1.reset_index().drop(df1.index[0]).T

new_df1
             1     2      3       4    5      6       7
index    black  blue  green  orange  red  white  yellow
person1      Y     Y              Y    Y      Y        
person2      Y     Y      Y            Y               
person3                   Y                           Y

Dashboards in Python Using Dash — Creating a Data Table using , Dashboards in Python Using Dash — Creating a Data Table using DataTable( columns, data) syntax to display the Reddit data. If your cells contain text with spaces, then you can overflow your content into multiple lines. Pandas: Convert a dataframe column into a list using Series.to_list() or numpy.ndarray.tolist() in python; Pandas : Get unique values in columns of a Dataframe in Python; How to convert Dataframe column type from string to date time; Pandas : Convert Dataframe index into column using dataframe.reset_index() in python; Python Pandas : Select

How to Create Pandas DataFrame in Python, Skip to content By typing the values in Python itself to create the DataFrame; By importing the values from a file (such as an Excel file), import pandas as pd data = {'First Column Name': ['First value', 'Second value',. Let's say that you have the following table stored in an Excel file (where the Excel file name is 'Cars' ):� How can we create data columns in Dash Table dynamically using callback with a function providing the dataframe Asked: Jul 26,2020 In: Python. 2 Answers. Emmanuel

Using Python to Write a Create Table Statement, Learn how to examine Stack Overflow data by writing a Python script, with a database, it can be a pain to write a create table statement and load your They' re available in a CSV format that's a daunting 158 columns wide. If i have data like Col1 A B A B A C I need output like Col_value Count A 3 B 2 C 1 I need to col_value and count be column names

The Pandas DataFrame: Make Working With Data Delightful – Real , The Pandas DataFrame: Make Working With Data Delightful. by Mirko Stojiljković Table of Contents. Introducing the In this table, the first row contains the column labels ( name , city , age , and py-score ). The first column� If you want to load the data into Redshift and rather than be generous with the data types, use the proper columns, you can speed up the load process by writing a Python script. Importing Libraries and Reading Data in Python. The first step is to load the data, import libraries, and load the data into a CSV reader object.

Comments
  • How is this data stored? In a file? data frame?
  • Hey, stored as a csv file so excel table format
  • ansev, for you first method to work i needed to run new_df1 = new_df1.drop(new_df1.columns[0], axis=1) . What version are you running? I upvoted you.
  • I use 0.25.2, You can also try: new_df=df.apply(lambda x: x.value_counts()).replace({1:'Y',np.nan:''}).T. Thanks by upvote:)