You can use the assign function:

df = df.assign(industry='yyy')
Answer from Mina HE on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-set-cell-value-in-pandas-dataframe
How to Set Cell Value in Pandas DataFrame? - GeeksforGeeks
July 23, 2025 - Here we are using the Pandas loc() method to set the column value based on row index and column name ... # create a dataframe # with 3 rows and 3 columns data = pd.DataFrame({ 'name': ['sireesha', 'ravi', 'rohith', 'pinkey', 'gnanesh'], 'subjects': ...
Discussions

python - How to set the columns in pandas - Stack Overflow
Here is my dataframe: Dec-18 Jan-19 Feb-19 Mar-19 Apr-19 May-19 Saturday 2540.0 2441.0 3832.0 4093.0 1455.0 2552.0 Sunday 1313.0 1891.0 2968.0 2260.0 1454.0 1798.0 More on stackoverflow.com
🌐 stackoverflow.com
July 2, 2019
python - Create a set from a series in pandas - Stack Overflow
I have a dataframe extracted from Kaggle's San Fransico Salaries: https://www.kaggle.com/kaggle/sf-salaries and I wish to create a set of the values of a column, for instance 'Status'. This is wha... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I set values to a column of pandas DataFrame? - Stack Overflow
For example, I got a df like: x y A 1 1 B 2 2 C 3 3 I want to change column 'y' values like this: x y A 1 56 B 2 45 C 3 34 I had the values in an ar... More on stackoverflow.com
🌐 stackoverflow.com
Convert dataframe rows into sets
I'm curious what you want this for! I've assumed your dataframe comes with each of your variables in their own column so this starts with a bit to combine a row into a tuple. Then it aggregates the tuples belonging to each set. #Make your example dataframe data=[["set1","a",9,10], ["set1","b",14,100], ["set2","c",5,69], ["set2","d",4,100]] df=pd.DataFrame(columns=["Set","var1","var2","var3"],data=data) #turn your columns into tuples df["tuple"]=list(df[["var1","var2","var3"]].to_records()) #combine df=df.groupby("Set")["tuple"].agg(lambda x: [y for y in x]).reset_index() More on reddit.com
🌐 r/learnpython
13
4
June 30, 2021
🌐
Finxter
blog.finxter.com › 5-best-ways-to-convert-pandas-dataframe-column-values-to-a-set
5 Best Ways to Convert Pandas DataFrame Column Values to a Set – Be on the Right Side of Change
February 19, 2024 - This method involves directly converting the column values into a list and then casting it to a set. The set() function is a Python built-in that creates a set from an iterable. This method is straightforward and the go-to for a quick conversion. ... import pandas as pd # Creating a pandas ...
🌐
Medium
medium.com › @whyamit101 › pandas-set-column-names-a-comprehensive-guide-130c84f8761a
Pandas Set Column Names: A Comprehensive Guide | by why amit | Medium
April 12, 2025 - When you work with data in Python, specifically using the pandas library, one of the first things you might want to do is set or change the column names of your DataFrame.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.set_index.html
pandas.DataFrame.set_index — pandas 3.0.3 documentation
Whether to append columns to existing index. Setting to True will add the new columns to existing index.
Find elsewhere
🌐
Built In
builtin.com › data-science › pandas-add-column
How to Add Columns in a Pandas DataFrame | Built In
Summary: Five methods for adding columns to a Pandas DataFrame include direct assignment, insert(), .loc[], .assign() and Python dictionary mapping. Each approach includes concise examples for quick application in data workflows. more Five methods ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.assign.html
pandas.DataFrame.assign — pandas 3.0.3 documentation
The column names are keywords. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas doesn’t check it). If the values are not callable, (e.g.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.html
pandas.DataFrame — pandas 3.0.3 documentation - PyData |
If data contains column labels, will perform column selection instead. ... Data type to force. Only a single dtype is allowed. If None, infer.
🌐
TutorialKart
tutorialkart.com › python › pandas › pandas-dataframe-set-column-names
How to set Column Names for DataFrame in Pandas?
July 9, 2021 - To set column names of DataFrame in Pandas, use pandas.DataFrame.columns attribute. Assign required column names as a list to this attribute.
🌐
IncludeHelp
includehelp.com › python › create-a-set-from-a-series-in-pandas.aspx
Python - Create a set from a series in pandas
To create a set from a series in pandas, you have to first find the unique elements using the series.unique() method and then convert it into a set by using the set() method which is an inbuilt method in Python.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Add rows/columns to DataFrame with assign(), insert() | note.nkmk.me
August 1, 2023 - The sample code in this article uses pandas version 2.0.3. ... You can select a column using [column_name] and assign values to it.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.astype.html
pandas.DataFrame.astype — pandas 3.0.3 documentation
Alternatively, use a mapping, e.g. {col: dtype, …}, where col is a column label and dtype is a numpy.dtype or Python type to cast one or more of the DataFrame’s columns to column-specific types. ... This keyword is now ignored; changing its value will have no impact on the method. Deprecated since version 3.0.0: This keyword is ignored and will be removed in pandas 4.0.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.assign.html
pandas.DataFrame.assign — pandas 3.0.1 documentation
October 17, 2021 - The column names are keywords. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas doesn’t check it). If the values are not callable, (e.g.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › create a set from a series in pandas
Create a Set From a Series in Pandas - Spark By {Examples}
March 27, 2024 - We can create a set from a series of pandas by using set(), Series.unique() function. The set object is used to store multiple items which are
🌐
Reddit
reddit.com › r/learnpython › convert dataframe rows into sets
r/learnpython on Reddit: Convert dataframe rows into sets
June 30, 2021 -

How can I convert my pandas dataframe into this format?

``` sets items weight value

0 set1 a 9 10

1 set1 b 14 100

2 set2 c 5 69

3 set2 d 4 100

Outcome i'm looking for:

set1 = (("a", 9, 10), ("b", 14, 100))

set2 = (("c", 5, 69), ("d", 4, 100))

print(set1)

set1 = (("a", 9, 10), ("b", 14, 100))

Top answer
1 of 2
3
I'm curious what you want this for! I've assumed your dataframe comes with each of your variables in their own column so this starts with a bit to combine a row into a tuple. Then it aggregates the tuples belonging to each set. #Make your example dataframe data=[["set1","a",9,10], ["set1","b",14,100], ["set2","c",5,69], ["set2","d",4,100]] df=pd.DataFrame(columns=["Set","var1","var2","var3"],data=data) #turn your columns into tuples df["tuple"]=list(df[["var1","var2","var3"]].to_records()) #combine df=df.groupby("Set")["tuple"].agg(lambda x: [y for y in x]).reset_index()
2 of 2
2
Note that in your example your set1 and set2 are actually tuples, not sets. I'll assume tuples/lists are what you want, and that you just mean "set" in the mathematical sense as a collection of related elements rather than an actual Python set. I don't think it's possible to use the values in a column as variable names, which is what it seems you want to do with your sets column. Someone will correct me if I'm wrong on that, I hope. However, the logic for creating nested groups based on the sets column is as follows: >>> [g.drop(columns=['sets']).values.tolist() for _, g in df.groupby('sets')] [[['a', 9, 10], ['b', 14, 100]], [['c', 5, 69], ['d', 4, 100]]] Alternatively, if you do want to be able to query your data by set name, different from but similar to print(set1), you can do it this way: >>> sets = df.set_index('sets').groupby('sets').apply(pd.Series.tolist) >>> sets sets set1 [[a, 9, 10], [b, 14, 100]] set2 [[c, 5, 69], [d, 4, 100]] >>> print(sets['set1']) [['a', 9, 10], ['b', 14, 100]]
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › insert
Python Pandas DataFrame insert() - Insert Column | Vultr Docs
January 1, 2025 - The insert() method in Pandas is a dynamic way to add a column at a specific location within a DataFrame.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › python-creating-a-pandas-dataframe-column-based-on-a-given-condition
Python | Creating a Pandas dataframe column based on a given condition - GeeksforGeeks
October 30, 2025 - df['Event'].apply(...): applies the lambda function to each value in the Event column. Create a New Column in Pandas DataFrame based on the Existing Columns