'How to create variables based on column names in dataframe?
I wanted to create variables in python based on the column names of my dataframe. Not sure if this is possible as I am quite new to Python. Lets say my df looks like this:
ID Date Name Counts
1 04/04/2018 JUOM 150
2 05/04/2018 1XMK 22
3 05/04/2018 N2IS 130
How could I create one variable where I can reference the Date and the Name columns from this dataframe?
Solution 1:[1]
If you want a variable x
assigned to the columns Date
and Name
, you can subset the data frame with the using df[['col1','col2',...]]
syntax. The outer brackets are the indexer operation of pandas data frame object, while the inner brackets create a list of desired column names.
x= df[['Date','Name']]
Which returns:
Date Name
0 04/04/2018 JUOM
1 05/04/2018 1XMK
2 05/04/2018 N2IS
If you don't want to have the data in a data frame, you could get the data with .values
property.
x= df[['Date','Name']].values
array([['04/04/2018', 'JUOM'],
['05/04/2018', '1XMK'],
['05/04/2018', 'N2IS']], dtype=object)
If you are using pandas version 0.24.0, they recommend replacing .values
with to_numpy()
.
Solution 2:[2]
You can do this:
var1 = df.columns[0]
var2 = df.columns[1]
The result:
var1 = ID
var2 = Date
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Jade Cacho |