Rename default column name in DataFrame [duplicate] - pandas
I want to change the column labels of a Pandas DataFrame from
['$a', '$b', '$c', '$d', '$e']
to
['a', 'b', 'c', 'd', 'e']
Rename Specific Columns
Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed:
df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'})
# Or rename the existing DataFrame (rather than creating a copy)
df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True)
Minimal Code Example
df = pd.DataFrame('x', index=range(3), columns=list('abcde'))
df
a b c d e
0 x x x x x
1 x x x x x
2 x x x x x
The following methods all work and produce the same output:
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1) # new method
df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns')
df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) # old method
df2
X Y c d e
0 x x x x x
1 x x x x x
2 x x x x x
Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True:
df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True)
df
X Y c d e
0 x x x x x
1 x x x x x
2 x x x x x
From v0.25, you can also specify errors='raise' to raise errors if an invalid column-to-rename is specified. See v0.25 rename() docs.
Reassign Column Headers
Use df.set_axis() with axis=1 and inplace=False (to return a copy).
df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1, inplace=False)
df2
V W X Y Z
0 x x x x x
1 x x x x x
2 x x x x x
This returns a copy, but you can modify the DataFrame in-place by setting inplace=True (this is the default behaviour for versions <=0.24 but is likely to change in the future).
You can also assign headers directly:
df.columns = ['V', 'W', 'X', 'Y', 'Z']
df
V W X Y Z
0 x x x x x
1 x x x x x
2 x x x x x
Just assign it to the .columns attribute:
>>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]})
>>> df
$a $b
0 1 10
1 2 20
>>> df.columns = ['a', 'b']
>>> df
a b
0 1 10
1 2 20
The rename method can take a function, for example:
In [11]: df.columns
Out[11]: Index([u'$a', u'$b', u'$c', u'$d', u'$e'], dtype=object)
In [12]: df.rename(columns=lambda x: x[1:], inplace=True)
In [13]: df.columns
Out[13]: Index([u'a', u'b', u'c', u'd', u'e'], dtype=object)
As documented in Working with text data:
df.columns = df.columns.str.replace('$', '')
Pandas 0.21+ Answer
There have been some significant updates to column renaming in version 0.21.
The rename method has added the axis parameter which may be set to columns or 1. This update makes this method match the rest of the pandas API. It still has the index and columns parameters but you are no longer forced to use them.
The set_axis method with the inplace set to False enables you to rename all the index or column labels with a list.
Examples for Pandas 0.21+
Construct sample DataFrame:
df = pd.DataFrame({'$a':[1,2], '$b': [3,4],
'$c':[5,6], '$d':[7,8],
'$e':[9,10]})
$a $b $c $d $e
0 1 3 5 7 9
1 2 4 6 8 10
Using rename with axis='columns' or axis=1
df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis='columns')
or
df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis=1)
Both result in the following:
a b c d e
0 1 3 5 7 9
1 2 4 6 8 10
It is still possible to use the old method signature:
df.rename(columns={'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'})
The rename function also accepts functions that will be applied to each column name.
df.rename(lambda x: x[1:], axis='columns')
or
df.rename(lambda x: x[1:], axis=1)
Using set_axis with a list and inplace=False
You can supply a list to the set_axis method that is equal in length to the number of columns (or index). Currently, inplace defaults to True, but inplace will be defaulted to False in future releases.
df.set_axis(['a', 'b', 'c', 'd', 'e'], axis='columns', inplace=False)
or
df.set_axis(['a', 'b', 'c', 'd', 'e'], axis=1, inplace=False)
Why not use df.columns = ['a', 'b', 'c', 'd', 'e']?
There is nothing wrong with assigning columns directly like this. It is a perfectly good solution.
The advantage of using set_axis is that it can be used as part of a method chain and that it returns a new copy of the DataFrame. Without it, you would have to store your intermediate steps of the chain to another variable before reassigning the columns.
# new for pandas 0.21+
df.some_method1()
.some_method2()
.set_axis()
.some_method3()
# old way
df1 = df.some_method1()
.some_method2()
df1.columns = columns
df1.some_method3()
Since you only want to remove the $ sign in all column names, you could just do:
df = df.rename(columns=lambda x: x.replace('$', ''))
OR
df.rename(columns=lambda x: x.replace('$', ''), inplace=True)
Renaming columns in Pandas is an easy task.
df.rename(columns={'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}, inplace=True)
df.columns = ['a', 'b', 'c', 'd', 'e']
It will replace the existing names with the names you provide, in the order you provide.
Use:
old_names = ['$a', '$b', '$c', '$d', '$e']
new_names = ['a', 'b', 'c', 'd', 'e']
df.rename(columns=dict(zip(old_names, new_names)), inplace=True)
This way you can manually edit the new_names as you wish. It works great when you need to rename only a few columns to correct misspellings, accents, remove special characters, etc.
One line or Pipeline solutions
I'll focus on two things:
OP clearly states
I have the edited column names stored it in a list, but I don't know how to replace the column names.
I do not want to solve the problem of how to replace '$' or strip the first character off of each column header. OP has already done this step. Instead I want to focus on replacing the existing columns object with a new one given a list of replacement column names.
df.columns = new where new is the list of new columns names is as simple as it gets. The drawback of this approach is that it requires editing the existing dataframe's columns attribute and it isn't done inline. I'll show a few ways to perform this via pipelining without editing the existing dataframe.
Setup 1
To focus on the need to rename of replace column names with a pre-existing list, I'll create a new sample dataframe df with initial column names and unrelated new column names.
df = pd.DataFrame({'Jack': [1, 2], 'Mahesh': [3, 4], 'Xin': [5, 6]})
new = ['x098', 'y765', 'z432']
df
Jack Mahesh Xin
0 1 3 5
1 2 4 6
Solution 1
pd.DataFrame.rename
It has been said already that if you had a dictionary mapping the old column names to new column names, you could use pd.DataFrame.rename.
d = {'Jack': 'x098', 'Mahesh': 'y765', 'Xin': 'z432'}
df.rename(columns=d)
x098 y765 z432
0 1 3 5
1 2 4 6
However, you can easily create that dictionary and include it in the call to rename. The following takes advantage of the fact that when iterating over df, we iterate over each column name.
# Given just a list of new column names
df.rename(columns=dict(zip(df, new)))
x098 y765 z432
0 1 3 5
1 2 4 6
This works great if your original column names are unique. But if they are not, then this breaks down.
Setup 2
Non-unique columns
df = pd.DataFrame(
[[1, 3, 5], [2, 4, 6]],
columns=['Mahesh', 'Mahesh', 'Xin']
)
new = ['x098', 'y765', 'z432']
df
Mahesh Mahesh Xin
0 1 3 5
1 2 4 6
Solution 2
pd.concat using the keys argument
First, notice what happens when we attempt to use solution 1:
df.rename(columns=dict(zip(df, new)))
y765 y765 z432
0 1 3 5
1 2 4 6
We didn't map the new list as the column names. We ended up repeating y765. Instead, we can use the keys argument of the pd.concat function while iterating through the columns of df.
pd.concat([c for _, c in df.items()], axis=1, keys=new)
x098 y765 z432
0 1 3 5
1 2 4 6
Solution 3
Reconstruct. This should only be used if you have a single dtype for all columns. Otherwise, you'll end up with dtype object for all columns and converting them back requires more dictionary work.
Single dtype
pd.DataFrame(df.values, df.index, new)
x098 y765 z432
0 1 3 5
1 2 4 6
Mixed dtype
pd.DataFrame(df.values, df.index, new).astype(dict(zip(new, df.dtypes)))
x098 y765 z432
0 1 3 5
1 2 4 6
Solution 4
This is a gimmicky trick with transpose and set_index. pd.DataFrame.set_index allows us to set an index inline, but there is no corresponding set_columns. So we can transpose, then set_index, and transpose back. However, the same single dtype versus mixed dtype caveat from solution 3 applies here.
Single dtype
df.T.set_index(np.asarray(new)).T
x098 y765 z432
0 1 3 5
1 2 4 6
Mixed dtype
df.T.set_index(np.asarray(new)).T.astype(dict(zip(new, df.dtypes)))
x098 y765 z432
0 1 3 5
1 2 4 6
Solution 5
Use a lambda in pd.DataFrame.rename that cycles through each element of new.
In this solution, we pass a lambda that takes x but then ignores it. It also takes a y but doesn't expect it. Instead, an iterator is given as a default value and I can then use that to cycle through one at a time without regard to what the value of x is.
df.rename(columns=lambda x, y=iter(new): next(y))
x098 y765 z432
0 1 3 5
1 2 4 6
And as pointed out to me by the folks in sopython chat, if I add a * in between x and y, I can protect my y variable. Though, in this context I don't believe it needs protecting. It is still worth mentioning.
df.rename(columns=lambda x, *, y=iter(new): next(y))
x098 y765 z432
0 1 3 5
1 2 4 6
Column names vs Names of Series
I would like to explain a bit what happens behind the scenes.
Dataframes are a set of Series.
Series in turn are an extension of a numpy.array.
numpy.arrays have a property .name.
This is the name of the series. It is seldom that Pandas respects this attribute, but it lingers in places and can be used to hack some Pandas behaviors.
Naming the list of columns
A lot of answers here talks about the df.columns attribute being a list when in fact it is a Series. This means it has a .name attribute.
This is what happens if you decide to fill in the name of the columns Series:
df.columns = ['column_one', 'column_two']
df.columns.names = ['name of the list of columns']
df.index.names = ['name of the index']
name of the list of columns column_one column_two
name of the index
0 4 1
1 5 2
2 6 3
Note that the name of the index always comes one column lower.
Artefacts that linger
The .name attribute lingers on sometimes. If you set df.columns = ['one', 'two'] then the df.one.name will be 'one'.
If you set df.one.name = 'three' then df.columns will still give you ['one', 'two'], and df.one.name will give you 'three'.
BUT
pd.DataFrame(df.one) will return
three
0 1
1 2
2 3
Because Pandas reuses the .name of the already defined Series.
Multi-level column names
Pandas has ways of doing multi-layered column names. There is not so much magic involved, but I wanted to cover this in my answer too since I don't see anyone picking up on this here.
|one |
|one |two |
0 | 4 | 1 |
1 | 5 | 2 |
2 | 6 | 3 |
This is easily achievable by setting columns to lists, like this:
df.columns = [['one', 'one'], ['one', 'two']]
Many of pandas functions have an inplace parameter. When setting it True, the transformation applies directly to the dataframe that you are calling it on. For example:
df = pd.DataFrame({'$a':[1,2], '$b': [3,4]})
df.rename(columns={'$a': 'a'}, inplace=True)
df.columns
>>> Index(['a', '$b'], dtype='object')
Alternatively, there are cases where you want to preserve the original dataframe. I have often seen people fall into this case if creating the dataframe is an expensive task. For example, if creating the dataframe required querying a snowflake database. In this case, just make sure the the inplace parameter is set to False.
df = pd.DataFrame({'$a':[1,2], '$b': [3,4]})
df2 = df.rename(columns={'$a': 'a'}, inplace=False)
df.columns
>>> Index(['$a', '$b'], dtype='object')
df2.columns
>>> Index(['a', '$b'], dtype='object')
If these types of transformations are something that you do often, you could also look into a number of different pandas GUI tools. I'm the creator of one called Mito. It’s a spreadsheet that automatically converts your edits to python code.
Let's understand renaming by a small example...
Renaming columns using mapping:
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) # Creating a df with column name A and B
df.rename({"A": "new_a", "B": "new_b"}, axis='columns', inplace =True) # Renaming column A with 'new_a' and B with 'new_b'
Output:
new_a new_b
0 1 4
1 2 5
2 3 6
Renaming index/Row_Name using mapping:
df.rename({0: "x", 1: "y", 2: "z"}, axis='index', inplace =True) # Row name are getting replaced by 'x', 'y', and 'z'.
Output:
new_a new_b
x 1 4
y 2 5
z 3 6
Suppose your dataset name is df, and df has.
df = ['$a', '$b', '$c', '$d', '$e']`
So, to rename these, we would simply do.
df.columns = ['a','b','c','d','e']
Let's say this is your dataframe.
You can rename the columns using two methods.
Using dataframe.columns=[#list]
df.columns=['a','b','c','d','e']
The limitation of this method is that if one column has to be changed, full column list has to be passed. Also, this method is not applicable on index labels.
For example, if you passed this:
df.columns = ['a','b','c','d']
This will throw an error. Length mismatch: Expected axis has 5 elements, new values have 4 elements.
Another method is the Pandas rename() method which is used to rename any index, column or row
df = df.rename(columns={'$a':'a'})
Similarly, you can change any rows or columns.
If you've got the dataframe, df.columns dumps everything into a list you can manipulate and then reassign into your dataframe as the names of columns...
columns = df.columns
columns = [row.replace("$", "") for row in columns]
df.rename(columns=dict(zip(columns, things)), inplace=True)
df.head() # To validate the output
Best way? I don't know. A way - yes.
A better way of evaluating all the main techniques put forward in the answers to the question is below using cProfile to gage memory and execution time. #kadee, #kaitlyn, and #eumiro had the functions with the fastest execution times - though these functions are so fast we're comparing the rounding of 0.000 and 0.001 seconds for all the answers. Moral: my answer above likely isn't the 'best' way.
import pandas as pd
import cProfile, pstats, re
old_names = ['$a', '$b', '$c', '$d', '$e']
new_names = ['a', 'b', 'c', 'd', 'e']
col_dict = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}
df = pd.DataFrame({'$a':[1, 2], '$b': [10, 20], '$c': ['bleep', 'blorp'], '$d': [1, 2], '$e': ['texa$', '']})
df.head()
def eumiro(df, nn):
df.columns = nn
# This direct renaming approach is duplicated in methodology in several other answers:
return df
def lexual1(df):
return df.rename(columns=col_dict)
def lexual2(df, col_dict):
return df.rename(columns=col_dict, inplace=True)
def Panda_Master_Hayden(df):
return df.rename(columns=lambda x: x[1:], inplace=True)
def paulo1(df):
return df.rename(columns=lambda x: x.replace('$', ''))
def paulo2(df):
return df.rename(columns=lambda x: x.replace('$', ''), inplace=True)
def migloo(df, on, nn):
return df.rename(columns=dict(zip(on, nn)), inplace=True)
def kadee(df):
return df.columns.str.replace('$', '')
def awo(df):
columns = df.columns
columns = [row.replace("$", "") for row in columns]
return df.rename(columns=dict(zip(columns, '')), inplace=True)
def kaitlyn(df):
df.columns = [col.strip('$') for col in df.columns]
return df
print 'eumiro'
cProfile.run('eumiro(df, new_names)')
print 'lexual1'
cProfile.run('lexual1(df)')
print 'lexual2'
cProfile.run('lexual2(df, col_dict)')
print 'andy hayden'
cProfile.run('Panda_Master_Hayden(df)')
print 'paulo1'
cProfile.run('paulo1(df)')
print 'paulo2'
cProfile.run('paulo2(df)')
print 'migloo'
cProfile.run('migloo(df, old_names, new_names)')
print 'kadee'
cProfile.run('kadee(df)')
print 'awo'
cProfile.run('awo(df)')
print 'kaitlyn'
cProfile.run('kaitlyn(df)')
df = pd.DataFrame({'$a': [1], '$b': [1], '$c': [1], '$d': [1], '$e': [1]})
If your new list of columns is in the same order as the existing columns, the assignment is simple:
new_cols = ['a', 'b', 'c', 'd', 'e']
df.columns = new_cols
>>> df
a b c d e
0 1 1 1 1 1
If you had a dictionary keyed on old column names to new column names, you could do the following:
d = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}
df.columns = df.columns.map(lambda col: d[col]) # Or `.map(d.get)` as pointed out by #PiRSquared.
>>> df
a b c d e
0 1 1 1 1 1
If you don't have a list or dictionary mapping, you could strip the leading $ symbol via a list comprehension:
df.columns = [col[1:] if col[0] == '$' else col for col in df]
df.rename(index=str, columns={'A':'a', 'B':'b'})
pandas.DataFrame.rename
If you already have a list for the new column names, you can try this:
new_cols = ['a', 'b', 'c', 'd', 'e']
new_names_map = {df.columns[i]:new_cols[i] for i in range(len(new_cols))}
df.rename(new_names_map, axis=1, inplace=True)
Another way we could replace the original column labels is by stripping the unwanted characters (here '$') from the original column labels.
This could have been done by running a for loop over df.columns and appending the stripped columns to df.columns.
Instead, we can do this neatly in a single statement by using list comprehension like below:
df.columns = [col.strip('$') for col in df.columns]
(strip method in Python strips the given character from beginning and end of the string.)
It is real simple. Just use:
df.columns = ['Name1', 'Name2', 'Name3'...]
And it will assign the column names by the order you put them in.
# This way it will work
import pandas as pd
# Define a dictionary
rankings = {'test': ['a'],
'odi': ['E'],
't20': ['P']}
# Convert the dictionary into DataFrame
rankings_pd = pd.DataFrame(rankings)
# Before renaming the columns
print(rankings_pd)
rankings_pd.rename(columns = {'test':'TEST'}, inplace = True)
You could use str.slice for that:
df.columns = df.columns.str.slice(1)
Another option is to rename using a regular expression:
import pandas as pd
import re
df = pd.DataFrame({'$a':[1,2], '$b':[3,4], '$c':[5,6]})
df = df.rename(columns=lambda x: re.sub('\$','',x))
>>> df
a b c
0 1 3 5
1 2 4 6
My method is generic wherein you can add additional delimiters by comma separating delimiters= variable and future-proof it.
Working Code:
import pandas as pd
import re
df = pd.DataFrame({'$a':[1,2], '$b': [3,4],'$c':[5,6], '$d': [7,8], '$e': [9,10]})
delimiters = '$'
matchPattern = '|'.join(map(re.escape, delimiters))
df.columns = [re.split(matchPattern, i)[1] for i in df.columns ]
Output:
>>> df
$a $b $c $d $e
0 1 3 5 7 9
1 2 4 6 8 10
>>> df
a b c d e
0 1 3 5 7 9
1 2 4 6 8 10
Note that the approaches in previous answers do not work for a MultiIndex. For a MultiIndex, you need to do something like the following:
>>> df = pd.DataFrame({('$a','$x'):[1,2], ('$b','$y'): [3,4], ('e','f'):[5,6]})
>>> df
$a $b e
$x $y f
0 1 3 5
1 2 4 6
>>> rename = {('$a','$x'):('a','x'), ('$b','$y'):('b','y')}
>>> df.columns = pandas.MultiIndex.from_tuples([
rename.get(item, item) for item in df.columns.tolist()])
>>> df
a b e
x y f
0 1 3 5
1 2 4 6
If you have to deal with loads of columns named by the providing system out of your control, I came up with the following approach that is a combination of a general approach and specific replacements in one go.
First create a dictionary from the dataframe column names using regular expressions in order to throw away certain appendixes of column names and then add specific replacements to the dictionary to name core columns as expected later in the receiving database.
This is then applied to the dataframe in one go.
dict = dict(zip(df.columns, df.columns.str.replace('(:S$|:C1$|:L$|:D$|\.Serial:L$)', '')))
dict['brand_timeseries:C1'] = 'BTS'
dict['respid:L'] = 'RespID'
dict['country:C1'] = 'CountryID'
dict['pim1:D'] = 'pim_actual'
df.rename(columns=dict, inplace=True)
If you just want to remove the '$' sign then use the below code
df.columns = pd.Series(df.columns.str.replace("$", ""))
In addition to the solution already provided, you can replace all the columns while you are reading the file. We can use names and header=0 to do that.
First, we create a list of the names that we like to use as our column names:
import pandas as pd
ufo_cols = ['city', 'color reported', 'shape reported', 'state', 'time']
ufo.columns = ufo_cols
ufo = pd.read_csv('link to the file you are using', names = ufo_cols, header = 0)
In this case, all the column names will be replaced with the names you have in your list.
Here's a nifty little function I like to use to cut down on typing:
def rename(data, oldnames, newname):
if type(oldnames) == str: # Input can be a string or list of strings
oldnames = [oldnames] # When renaming multiple columns
newname = [newname] # Make sure you pass the corresponding list of new names
i = 0
for name in oldnames:
oldvar = [c for c in data.columns if name in c]
if len(oldvar) == 0:
raise ValueError("Sorry, couldn't find that column in the dataset")
if len(oldvar) > 1: # Doesn't have to be an exact match
print("Found multiple columns that matched " + str(name) + ": ")
for c in oldvar:
print(str(oldvar.index(c)) + ": " + str(c))
ind = input('Please enter the index of the column you would like to rename: ')
oldvar = oldvar[int(ind)]
if len(oldvar) == 1:
oldvar = oldvar[0]
data = data.rename(columns = {oldvar : newname[i]})
i += 1
return data
Here is an example of how it works:
In [2]: df = pd.DataFrame(np.random.randint(0, 10, size=(10, 4)), columns = ['col1', 'col2', 'omg', 'idk'])
# First list = existing variables
# Second list = new names for those variables
In [3]: df = rename(df, ['col', 'omg'],['first', 'ohmy'])
Found multiple columns that matched col:
0: col1
1: col2
Please enter the index of the column you would like to rename: 0
In [4]: df.columns
Out[5]: Index(['first', 'col2', 'ohmy', 'idk'], dtype='object')
Related
Pandas aggregate to a list of dicts [duplicate]
I have a pandas data frame df like: a b A 1 A 2 B 5 B 5 B 4 C 6 I want to group by the first column and get second column as lists in rows: A [1,2] B [5,5,4] C [6] Is it possible to do something like this using pandas groupby?
You can do this using groupby to group on the column of interest and then apply list to every group: In [1]: df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]}) df Out[1]: a b 0 A 1 1 A 2 2 B 5 3 B 5 4 B 4 5 C 6 In [2]: df.groupby('a')['b'].apply(list) Out[2]: a A [1, 2] B [5, 5, 4] C [6] Name: b, dtype: object In [3]: df1 = df.groupby('a')['b'].apply(list).reset_index(name='new') df1 Out[3]: a new 0 A [1, 2] 1 B [5, 5, 4] 2 C [6]
A handy way to achieve this would be: df.groupby('a').agg({'b':lambda x: list(x)}) Look into writing Custom Aggregations: https://www.kaggle.com/akshaysehgal/how-to-group-by-aggregate-using-py
If performance is important go down to numpy level: import numpy as np df = pd.DataFrame({'a': np.random.randint(0, 60, 600), 'b': [1, 2, 5, 5, 4, 6]*100}) def f(df): keys, values = df.sort_values('a').values.T ukeys, index = np.unique(keys, True) arrays = np.split(values, index[1:]) df2 = pd.DataFrame({'a':ukeys, 'b':[list(a) for a in arrays]}) return df2 Tests: In [301]: %timeit f(df) 1000 loops, best of 3: 1.64 ms per loop In [302]: %timeit df.groupby('a')['b'].apply(list) 100 loops, best of 3: 5.26 ms per loop
To solve this for several columns of a dataframe: In [5]: df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6],'c' ...: :[3,3,3,4,4,4]}) In [6]: df Out[6]: a b c 0 A 1 3 1 A 2 3 2 B 5 3 3 B 5 4 4 B 4 4 5 C 6 4 In [7]: df.groupby('a').agg(lambda x: list(x)) Out[7]: b c a A [1, 2] [3, 3] B [5, 5, 4] [3, 4, 4] C [6] [4] This answer was inspired from Anamika Modi's answer. Thank you!
Use any of the following groupby and agg recipes. # Setup df = pd.DataFrame({ 'a': ['A', 'A', 'B', 'B', 'B', 'C'], 'b': [1, 2, 5, 5, 4, 6], 'c': ['x', 'y', 'z', 'x', 'y', 'z'] }) df a b c 0 A 1 x 1 A 2 y 2 B 5 z 3 B 5 x 4 B 4 y 5 C 6 z To aggregate multiple columns as lists, use any of the following: df.groupby('a').agg(list) df.groupby('a').agg(pd.Series.tolist) b c a A [1, 2] [x, y] B [5, 5, 4] [z, x, y] C [6] [z] To group-listify a single column only, convert the groupby to a SeriesGroupBy object, then call SeriesGroupBy.agg. Use, df.groupby('a').agg({'b': list}) # 4.42 ms df.groupby('a')['b'].agg(list) # 2.76 ms - faster a A [1, 2] B [5, 5, 4] C [6] Name: b, dtype: object
As you were saying the groupby method of a pd.DataFrame object can do the job. Example L = ['A','A','B','B','B','C'] N = [1,2,5,5,4,6] import pandas as pd df = pd.DataFrame(zip(L,N),columns = list('LN')) groups = df.groupby(df.L) groups.groups {'A': [0, 1], 'B': [2, 3, 4], 'C': [5]} which gives and index-wise description of the groups. To get elements of single groups, you can do, for instance groups.get_group('A') L N 0 A 1 1 A 2 groups.get_group('B') L N 2 B 5 3 B 5 4 B 4
It is time to use agg instead of apply . When df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6], 'c': [1,2,5,5,4,6]}) If you want multiple columns stack into list , result in pd.DataFrame df.groupby('a')[['b', 'c']].agg(list) # or df.groupby('a').agg(list) If you want single column in list, result in ps.Series df.groupby('a')['b'].agg(list) #or df.groupby('a')['b'].apply(list) Note, result in pd.DataFrame is about 10x slower than result in ps.Series when you only aggregate single column, use it in multicolumns case .
Just a suplement. pandas.pivot_table is much more universal and seems more convenient: """data""" df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6], 'c':[1,2,1,1,1,6]}) print(df) a b c 0 A 1 1 1 A 2 2 2 B 5 1 3 B 5 1 4 B 4 1 5 C 6 6 """pivot_table""" pt = pd.pivot_table(df, values=['b', 'c'], index='a', aggfunc={'b': list, 'c': set}) print(pt) b c a A [1, 2] {1, 2} B [5, 5, 4] {1} C [6] {6}
If looking for a unique list while grouping multiple columns this could probably help: df.groupby('a').agg(lambda x: list(set(x))).reset_index()
Building upon #B.M answer, here is a more general version and updated to work with newer library version: (numpy version 1.19.2, pandas version 1.2.1) And this solution can also deal with multi-indices: However this is not heavily tested, use with caution. If performance is important go down to numpy level: import pandas as pd import numpy as np np.random.seed(0) df = pd.DataFrame({'a': np.random.randint(0, 10, 90), 'b': [1,2,3]*30, 'c':list('abcefghij')*10, 'd': list('hij')*30}) def f_multi(df,col_names): if not isinstance(col_names,list): col_names = [col_names] values = df.sort_values(col_names).values.T col_idcs = [df.columns.get_loc(cn) for cn in col_names] other_col_names = [name for idx, name in enumerate(df.columns) if idx not in col_idcs] other_col_idcs = [df.columns.get_loc(cn) for cn in other_col_names] # split df into indexing colums(=keys) and data colums(=vals) keys = values[col_idcs,:] vals = values[other_col_idcs,:] # list of tuple of key pairs multikeys = list(zip(*keys)) # remember unique key pairs and ther indices ukeys, index = np.unique(multikeys, return_index=True, axis=0) # split data columns according to those indices arrays = np.split(vals, index[1:], axis=1) # resulting list of subarrays has same number of subarrays as unique key pairs # each subarray has the following shape: # rows = number of non-grouped data columns # cols = number of data points grouped into that unique key pair # prepare multi index idx = pd.MultiIndex.from_arrays(ukeys.T, names=col_names) list_agg_vals = dict() for tup in zip(*arrays, other_col_names): col_vals = tup[:-1] # first entries are the subarrays from above col_name = tup[-1] # last entry is data-column name list_agg_vals[col_name] = col_vals df2 = pd.DataFrame(data=list_agg_vals, index=idx) return df2 Tests: In [227]: %timeit f_multi(df, ['a','d']) 2.54 ms ± 64.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [228]: %timeit df.groupby(['a','d']).agg(list) 4.56 ms ± 61.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) Results: for the random seed 0 one would get:
The easiest way I have found to achieve the same thing, at least for one column, which is similar to Anamika's answer, just with the tuple syntax for the aggregate function. df.groupby('a').agg(b=('b','unique'), c=('c','unique'))
Let us using df.groupby with list and Series constructor pd.Series({x : y.b.tolist() for x , y in df.groupby('a')}) Out[664]: A [1, 2] B [5, 5, 4] C [6] dtype: object
Here I have grouped elements with "|" as a separator import pandas as pd df = pd.read_csv('input.csv') df Out[1]: Area Keywords 0 A 1 1 A 2 2 B 5 3 B 5 4 B 4 5 C 6 df.dropna(inplace = True) df['Area']=df['Area'].apply(lambda x:x.lower().strip()) print df.columns df_op = df.groupby('Area').agg({"Keywords":lambda x : "|".join(x)}) df_op.to_csv('output.csv') Out[2]: df_op Area Keywords A [1| 2] B [5| 5| 4] C [6]
Answer based on #EdChum's comment on his answer. Comment is this - groupby is notoriously slow and memory hungry, what you could do is sort by column A, then find the idxmin and idxmax (probably store this in a dict) and use this to slice your dataframe would be faster I think Let's first create a dataframe with 500k categories in first column and total df shape 20 million as mentioned in question. df = pd.DataFrame(columns=['a', 'b']) df['a'] = (np.random.randint(low=0, high=500000, size=(20000000,))).astype(str) df['b'] = list(range(20000000)) print(df.shape) df.head() # Sort data by first column df.sort_values(by=['a'], ascending=True, inplace=True) df.reset_index(drop=True, inplace=True) # Create a temp column df['temp_idx'] = list(range(df.shape[0])) # Take all values of b in a separate list all_values_b = list(df.b.values) print(len(all_values_b)) # For each category in column a, find min and max indexes gp_df = df.groupby(['a']).agg({'temp_idx': [np.min, np.max]}) gp_df.reset_index(inplace=True) gp_df.columns = ['a', 'temp_idx_min', 'temp_idx_max'] # Now create final list_b column, using min and max indexes for each category of a and filtering list of b. gp_df['list_b'] = gp_df[['temp_idx_min', 'temp_idx_max']].apply(lambda x: all_values_b[x[0]:x[1]+1], axis=1) print(gp_df.shape) gp_df.head() This above code takes 2 minutes for 20 million rows and 500k categories in first column.
Sorting consumes O(nlog(n)) time which is the most time consuming operation in the solutions suggested above For a simple solution (containing single column) pd.Series.to_list would work and can be considered more efficient unless considering other frameworks e.g. import pandas as pd from string import ascii_lowercase import random def generate_string(case=4): return ''.join([random.choice(ascii_lowercase) for _ in range(case)]) df = pd.DataFrame({'num_val':[random.randint(0,100) for _ in range(20000000)],'string_val':[generate_string() for _ in range(20000000)]}) %timeit df.groupby('string_val').agg({'num_val':pd.Series.to_list}) For 20 million records it takes about 17.2 seconds. compared to apply(list) which takes about 19.2 and lambda function which takes about 20.6s
Just to add up to previous answers, In my case, I want the list and other functions like min and max. The way to do that is: df = pd.DataFrame({ 'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6] }) df=df.groupby('a').agg({ 'b':['min', 'max',lambda x: list(x)] }) #then flattening and renaming if necessary df.columns = df.columns.to_flat_index() df.rename(columns={('b', 'min'): 'b_min', ('b', 'max'): 'b_max', ('b', '<lambda_0>'): 'b_list'},inplace=True)
It's a bit old but I was directed here. Is there anyway to group it by multiple different columns? "column1", "column2", "column3" "foo", "val1", 3 "foo", "val2", 0 "foo", "val2", 3 "bar", "other", 99 to this: "column1", "column2", "column3" "foo", "val1", [ 3 ] "foo", "val2", [ 0, 3 ] "bar", "other", [ 99 ]
drop rows from a Pandas dataframe based on which rows have missing values in another dataframe
I'm trying to drop rows with missing values in any of several dataframes. They all have the same number of rows, so I tried this: model_data_with_NA = pd.concat([other_df, standardized_numerical_data, encode_categorical_data], axis=1) ok_rows = ~(model_data_with_NA.isna().all(axis=1)) model_data = model_data_with_NA.dropna() assert(sum(ok_rows) == len(model_data)) False! As a newbie in Python, I wonder why this doesn't work? Also, is it better to use hierarchical indexing? Then I can extract the original columns from model_data.
In Short I believe the all in ~(model_data_with_NA.isna().all(axis=1)) should be replaced with any. The reason is that all checks here if every value in a row is missing, and any checks if one of the values is missing. Full Example import pandas as pd import numpy as np df1 = pd.DataFrame({'a':[1, 2, 3]}) df2 = pd.DataFrame({'b':[1, np.nan]}) df3 = pd.DataFrame({'c': [1, 2, np.nan]}) model_data_with_na = pd.concat([df1, df2, df3], axis=1) ok_rows = ~(model_data_with_na.isna().any(axis=1)) model_data = model_data_with_na.dropna() assert(sum(ok_rows) == len(model_data)) model_data_with_na a b c 0 1 1 1 1 2 nan 2 2 3 nan nan model_data a b c 0 1 1 1
Select columns from data frame using 1, 0 list , pandas
I have a list of 1,0 where each element is corresponding to an index of a column on a data frame, for example: df.columns = ['a','b','c'] binary_list = [0,1,0] based on that I want to select only b column from the data frame as on my binary list it has 1 only corresponds to b is there a way to perform that in pandas? P.S this is my first time posting on stackoverflow, apologies if I am not following a specific styling
If the binary list is aligned with the columns, you can use boolean indexing: df = pd.DataFrame([[1, 2, 3]], columns=['a', 'b', 'c']) binary_list = [0,1,0] df.loc[:, map(bool, binary_list)] Output: b 0 2
Pandas - Is it possible to permanently change dataframe column labels to the default column numbers. The dataframe has at least 40 columns [duplicate]
I want to change the column labels of a Pandas DataFrame from ['$a', '$b', '$c', '$d', '$e'] to ['a', 'b', 'c', 'd', 'e']
Rename Specific Columns Use the df.rename() function and refer the columns to be renamed. Not all the columns have to be renamed: df = df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}) # Or rename the existing DataFrame (rather than creating a copy) df.rename(columns={'oldName1': 'newName1', 'oldName2': 'newName2'}, inplace=True) Minimal Code Example df = pd.DataFrame('x', index=range(3), columns=list('abcde')) df a b c d e 0 x x x x x 1 x x x x x 2 x x x x x The following methods all work and produce the same output: df2 = df.rename({'a': 'X', 'b': 'Y'}, axis=1) # new method df2 = df.rename({'a': 'X', 'b': 'Y'}, axis='columns') df2 = df.rename(columns={'a': 'X', 'b': 'Y'}) # old method df2 X Y c d e 0 x x x x x 1 x x x x x 2 x x x x x Remember to assign the result back, as the modification is not-inplace. Alternatively, specify inplace=True: df.rename({'a': 'X', 'b': 'Y'}, axis=1, inplace=True) df X Y c d e 0 x x x x x 1 x x x x x 2 x x x x x From v0.25, you can also specify errors='raise' to raise errors if an invalid column-to-rename is specified. See v0.25 rename() docs. Reassign Column Headers Use df.set_axis() with axis=1 and inplace=False (to return a copy). df2 = df.set_axis(['V', 'W', 'X', 'Y', 'Z'], axis=1, inplace=False) df2 V W X Y Z 0 x x x x x 1 x x x x x 2 x x x x x This returns a copy, but you can modify the DataFrame in-place by setting inplace=True (this is the default behaviour for versions <=0.24 but is likely to change in the future). You can also assign headers directly: df.columns = ['V', 'W', 'X', 'Y', 'Z'] df V W X Y Z 0 x x x x x 1 x x x x x 2 x x x x x
Just assign it to the .columns attribute: >>> df = pd.DataFrame({'$a':[1,2], '$b': [10,20]}) >>> df $a $b 0 1 10 1 2 20 >>> df.columns = ['a', 'b'] >>> df a b 0 1 10 1 2 20
The rename method can take a function, for example: In [11]: df.columns Out[11]: Index([u'$a', u'$b', u'$c', u'$d', u'$e'], dtype=object) In [12]: df.rename(columns=lambda x: x[1:], inplace=True) In [13]: df.columns Out[13]: Index([u'a', u'b', u'c', u'd', u'e'], dtype=object)
As documented in Working with text data: df.columns = df.columns.str.replace('$', '')
Pandas 0.21+ Answer There have been some significant updates to column renaming in version 0.21. The rename method has added the axis parameter which may be set to columns or 1. This update makes this method match the rest of the pandas API. It still has the index and columns parameters but you are no longer forced to use them. The set_axis method with the inplace set to False enables you to rename all the index or column labels with a list. Examples for Pandas 0.21+ Construct sample DataFrame: df = pd.DataFrame({'$a':[1,2], '$b': [3,4], '$c':[5,6], '$d':[7,8], '$e':[9,10]}) $a $b $c $d $e 0 1 3 5 7 9 1 2 4 6 8 10 Using rename with axis='columns' or axis=1 df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis='columns') or df.rename({'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}, axis=1) Both result in the following: a b c d e 0 1 3 5 7 9 1 2 4 6 8 10 It is still possible to use the old method signature: df.rename(columns={'$a':'a', '$b':'b', '$c':'c', '$d':'d', '$e':'e'}) The rename function also accepts functions that will be applied to each column name. df.rename(lambda x: x[1:], axis='columns') or df.rename(lambda x: x[1:], axis=1) Using set_axis with a list and inplace=False You can supply a list to the set_axis method that is equal in length to the number of columns (or index). Currently, inplace defaults to True, but inplace will be defaulted to False in future releases. df.set_axis(['a', 'b', 'c', 'd', 'e'], axis='columns', inplace=False) or df.set_axis(['a', 'b', 'c', 'd', 'e'], axis=1, inplace=False) Why not use df.columns = ['a', 'b', 'c', 'd', 'e']? There is nothing wrong with assigning columns directly like this. It is a perfectly good solution. The advantage of using set_axis is that it can be used as part of a method chain and that it returns a new copy of the DataFrame. Without it, you would have to store your intermediate steps of the chain to another variable before reassigning the columns. # new for pandas 0.21+ df.some_method1() .some_method2() .set_axis() .some_method3() # old way df1 = df.some_method1() .some_method2() df1.columns = columns df1.some_method3()
Since you only want to remove the $ sign in all column names, you could just do: df = df.rename(columns=lambda x: x.replace('$', '')) OR df.rename(columns=lambda x: x.replace('$', ''), inplace=True)
Renaming columns in Pandas is an easy task. df.rename(columns={'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'}, inplace=True)
df.columns = ['a', 'b', 'c', 'd', 'e'] It will replace the existing names with the names you provide, in the order you provide.
Use: old_names = ['$a', '$b', '$c', '$d', '$e'] new_names = ['a', 'b', 'c', 'd', 'e'] df.rename(columns=dict(zip(old_names, new_names)), inplace=True) This way you can manually edit the new_names as you wish. It works great when you need to rename only a few columns to correct misspellings, accents, remove special characters, etc.
One line or Pipeline solutions I'll focus on two things: OP clearly states I have the edited column names stored it in a list, but I don't know how to replace the column names. I do not want to solve the problem of how to replace '$' or strip the first character off of each column header. OP has already done this step. Instead I want to focus on replacing the existing columns object with a new one given a list of replacement column names. df.columns = new where new is the list of new columns names is as simple as it gets. The drawback of this approach is that it requires editing the existing dataframe's columns attribute and it isn't done inline. I'll show a few ways to perform this via pipelining without editing the existing dataframe. Setup 1 To focus on the need to rename of replace column names with a pre-existing list, I'll create a new sample dataframe df with initial column names and unrelated new column names. df = pd.DataFrame({'Jack': [1, 2], 'Mahesh': [3, 4], 'Xin': [5, 6]}) new = ['x098', 'y765', 'z432'] df Jack Mahesh Xin 0 1 3 5 1 2 4 6 Solution 1 pd.DataFrame.rename It has been said already that if you had a dictionary mapping the old column names to new column names, you could use pd.DataFrame.rename. d = {'Jack': 'x098', 'Mahesh': 'y765', 'Xin': 'z432'} df.rename(columns=d) x098 y765 z432 0 1 3 5 1 2 4 6 However, you can easily create that dictionary and include it in the call to rename. The following takes advantage of the fact that when iterating over df, we iterate over each column name. # Given just a list of new column names df.rename(columns=dict(zip(df, new))) x098 y765 z432 0 1 3 5 1 2 4 6 This works great if your original column names are unique. But if they are not, then this breaks down. Setup 2 Non-unique columns df = pd.DataFrame( [[1, 3, 5], [2, 4, 6]], columns=['Mahesh', 'Mahesh', 'Xin'] ) new = ['x098', 'y765', 'z432'] df Mahesh Mahesh Xin 0 1 3 5 1 2 4 6 Solution 2 pd.concat using the keys argument First, notice what happens when we attempt to use solution 1: df.rename(columns=dict(zip(df, new))) y765 y765 z432 0 1 3 5 1 2 4 6 We didn't map the new list as the column names. We ended up repeating y765. Instead, we can use the keys argument of the pd.concat function while iterating through the columns of df. pd.concat([c for _, c in df.items()], axis=1, keys=new) x098 y765 z432 0 1 3 5 1 2 4 6 Solution 3 Reconstruct. This should only be used if you have a single dtype for all columns. Otherwise, you'll end up with dtype object for all columns and converting them back requires more dictionary work. Single dtype pd.DataFrame(df.values, df.index, new) x098 y765 z432 0 1 3 5 1 2 4 6 Mixed dtype pd.DataFrame(df.values, df.index, new).astype(dict(zip(new, df.dtypes))) x098 y765 z432 0 1 3 5 1 2 4 6 Solution 4 This is a gimmicky trick with transpose and set_index. pd.DataFrame.set_index allows us to set an index inline, but there is no corresponding set_columns. So we can transpose, then set_index, and transpose back. However, the same single dtype versus mixed dtype caveat from solution 3 applies here. Single dtype df.T.set_index(np.asarray(new)).T x098 y765 z432 0 1 3 5 1 2 4 6 Mixed dtype df.T.set_index(np.asarray(new)).T.astype(dict(zip(new, df.dtypes))) x098 y765 z432 0 1 3 5 1 2 4 6 Solution 5 Use a lambda in pd.DataFrame.rename that cycles through each element of new. In this solution, we pass a lambda that takes x but then ignores it. It also takes a y but doesn't expect it. Instead, an iterator is given as a default value and I can then use that to cycle through one at a time without regard to what the value of x is. df.rename(columns=lambda x, y=iter(new): next(y)) x098 y765 z432 0 1 3 5 1 2 4 6 And as pointed out to me by the folks in sopython chat, if I add a * in between x and y, I can protect my y variable. Though, in this context I don't believe it needs protecting. It is still worth mentioning. df.rename(columns=lambda x, *, y=iter(new): next(y)) x098 y765 z432 0 1 3 5 1 2 4 6
Column names vs Names of Series I would like to explain a bit what happens behind the scenes. Dataframes are a set of Series. Series in turn are an extension of a numpy.array. numpy.arrays have a property .name. This is the name of the series. It is seldom that Pandas respects this attribute, but it lingers in places and can be used to hack some Pandas behaviors. Naming the list of columns A lot of answers here talks about the df.columns attribute being a list when in fact it is a Series. This means it has a .name attribute. This is what happens if you decide to fill in the name of the columns Series: df.columns = ['column_one', 'column_two'] df.columns.names = ['name of the list of columns'] df.index.names = ['name of the index'] name of the list of columns column_one column_two name of the index 0 4 1 1 5 2 2 6 3 Note that the name of the index always comes one column lower. Artefacts that linger The .name attribute lingers on sometimes. If you set df.columns = ['one', 'two'] then the df.one.name will be 'one'. If you set df.one.name = 'three' then df.columns will still give you ['one', 'two'], and df.one.name will give you 'three'. BUT pd.DataFrame(df.one) will return three 0 1 1 2 2 3 Because Pandas reuses the .name of the already defined Series. Multi-level column names Pandas has ways of doing multi-layered column names. There is not so much magic involved, but I wanted to cover this in my answer too since I don't see anyone picking up on this here. |one | |one |two | 0 | 4 | 1 | 1 | 5 | 2 | 2 | 6 | 3 | This is easily achievable by setting columns to lists, like this: df.columns = [['one', 'one'], ['one', 'two']]
Many of pandas functions have an inplace parameter. When setting it True, the transformation applies directly to the dataframe that you are calling it on. For example: df = pd.DataFrame({'$a':[1,2], '$b': [3,4]}) df.rename(columns={'$a': 'a'}, inplace=True) df.columns >>> Index(['a', '$b'], dtype='object') Alternatively, there are cases where you want to preserve the original dataframe. I have often seen people fall into this case if creating the dataframe is an expensive task. For example, if creating the dataframe required querying a snowflake database. In this case, just make sure the the inplace parameter is set to False. df = pd.DataFrame({'$a':[1,2], '$b': [3,4]}) df2 = df.rename(columns={'$a': 'a'}, inplace=False) df.columns >>> Index(['$a', '$b'], dtype='object') df2.columns >>> Index(['a', '$b'], dtype='object') If these types of transformations are something that you do often, you could also look into a number of different pandas GUI tools. I'm the creator of one called Mito. It’s a spreadsheet that automatically converts your edits to python code.
Let's understand renaming by a small example... Renaming columns using mapping: df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) # Creating a df with column name A and B df.rename({"A": "new_a", "B": "new_b"}, axis='columns', inplace =True) # Renaming column A with 'new_a' and B with 'new_b' Output: new_a new_b 0 1 4 1 2 5 2 3 6 Renaming index/Row_Name using mapping: df.rename({0: "x", 1: "y", 2: "z"}, axis='index', inplace =True) # Row name are getting replaced by 'x', 'y', and 'z'. Output: new_a new_b x 1 4 y 2 5 z 3 6
Suppose your dataset name is df, and df has. df = ['$a', '$b', '$c', '$d', '$e']` So, to rename these, we would simply do. df.columns = ['a','b','c','d','e']
Let's say this is your dataframe. You can rename the columns using two methods. Using dataframe.columns=[#list] df.columns=['a','b','c','d','e'] The limitation of this method is that if one column has to be changed, full column list has to be passed. Also, this method is not applicable on index labels. For example, if you passed this: df.columns = ['a','b','c','d'] This will throw an error. Length mismatch: Expected axis has 5 elements, new values have 4 elements. Another method is the Pandas rename() method which is used to rename any index, column or row df = df.rename(columns={'$a':'a'}) Similarly, you can change any rows or columns.
If you've got the dataframe, df.columns dumps everything into a list you can manipulate and then reassign into your dataframe as the names of columns... columns = df.columns columns = [row.replace("$", "") for row in columns] df.rename(columns=dict(zip(columns, things)), inplace=True) df.head() # To validate the output Best way? I don't know. A way - yes. A better way of evaluating all the main techniques put forward in the answers to the question is below using cProfile to gage memory and execution time. #kadee, #kaitlyn, and #eumiro had the functions with the fastest execution times - though these functions are so fast we're comparing the rounding of 0.000 and 0.001 seconds for all the answers. Moral: my answer above likely isn't the 'best' way. import pandas as pd import cProfile, pstats, re old_names = ['$a', '$b', '$c', '$d', '$e'] new_names = ['a', 'b', 'c', 'd', 'e'] col_dict = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'} df = pd.DataFrame({'$a':[1, 2], '$b': [10, 20], '$c': ['bleep', 'blorp'], '$d': [1, 2], '$e': ['texa$', '']}) df.head() def eumiro(df, nn): df.columns = nn # This direct renaming approach is duplicated in methodology in several other answers: return df def lexual1(df): return df.rename(columns=col_dict) def lexual2(df, col_dict): return df.rename(columns=col_dict, inplace=True) def Panda_Master_Hayden(df): return df.rename(columns=lambda x: x[1:], inplace=True) def paulo1(df): return df.rename(columns=lambda x: x.replace('$', '')) def paulo2(df): return df.rename(columns=lambda x: x.replace('$', ''), inplace=True) def migloo(df, on, nn): return df.rename(columns=dict(zip(on, nn)), inplace=True) def kadee(df): return df.columns.str.replace('$', '') def awo(df): columns = df.columns columns = [row.replace("$", "") for row in columns] return df.rename(columns=dict(zip(columns, '')), inplace=True) def kaitlyn(df): df.columns = [col.strip('$') for col in df.columns] return df print 'eumiro' cProfile.run('eumiro(df, new_names)') print 'lexual1' cProfile.run('lexual1(df)') print 'lexual2' cProfile.run('lexual2(df, col_dict)') print 'andy hayden' cProfile.run('Panda_Master_Hayden(df)') print 'paulo1' cProfile.run('paulo1(df)') print 'paulo2' cProfile.run('paulo2(df)') print 'migloo' cProfile.run('migloo(df, old_names, new_names)') print 'kadee' cProfile.run('kadee(df)') print 'awo' cProfile.run('awo(df)') print 'kaitlyn' cProfile.run('kaitlyn(df)')
df = pd.DataFrame({'$a': [1], '$b': [1], '$c': [1], '$d': [1], '$e': [1]}) If your new list of columns is in the same order as the existing columns, the assignment is simple: new_cols = ['a', 'b', 'c', 'd', 'e'] df.columns = new_cols >>> df a b c d e 0 1 1 1 1 1 If you had a dictionary keyed on old column names to new column names, you could do the following: d = {'$a': 'a', '$b': 'b', '$c': 'c', '$d': 'd', '$e': 'e'} df.columns = df.columns.map(lambda col: d[col]) # Or `.map(d.get)` as pointed out by #PiRSquared. >>> df a b c d e 0 1 1 1 1 1 If you don't have a list or dictionary mapping, you could strip the leading $ symbol via a list comprehension: df.columns = [col[1:] if col[0] == '$' else col for col in df]
df.rename(index=str, columns={'A':'a', 'B':'b'}) pandas.DataFrame.rename
If you already have a list for the new column names, you can try this: new_cols = ['a', 'b', 'c', 'd', 'e'] new_names_map = {df.columns[i]:new_cols[i] for i in range(len(new_cols))} df.rename(new_names_map, axis=1, inplace=True)
Another way we could replace the original column labels is by stripping the unwanted characters (here '$') from the original column labels. This could have been done by running a for loop over df.columns and appending the stripped columns to df.columns. Instead, we can do this neatly in a single statement by using list comprehension like below: df.columns = [col.strip('$') for col in df.columns] (strip method in Python strips the given character from beginning and end of the string.)
It is real simple. Just use: df.columns = ['Name1', 'Name2', 'Name3'...] And it will assign the column names by the order you put them in.
# This way it will work import pandas as pd # Define a dictionary rankings = {'test': ['a'], 'odi': ['E'], 't20': ['P']} # Convert the dictionary into DataFrame rankings_pd = pd.DataFrame(rankings) # Before renaming the columns print(rankings_pd) rankings_pd.rename(columns = {'test':'TEST'}, inplace = True)
You could use str.slice for that: df.columns = df.columns.str.slice(1)
Another option is to rename using a regular expression: import pandas as pd import re df = pd.DataFrame({'$a':[1,2], '$b':[3,4], '$c':[5,6]}) df = df.rename(columns=lambda x: re.sub('\$','',x)) >>> df a b c 0 1 3 5 1 2 4 6
My method is generic wherein you can add additional delimiters by comma separating delimiters= variable and future-proof it. Working Code: import pandas as pd import re df = pd.DataFrame({'$a':[1,2], '$b': [3,4],'$c':[5,6], '$d': [7,8], '$e': [9,10]}) delimiters = '$' matchPattern = '|'.join(map(re.escape, delimiters)) df.columns = [re.split(matchPattern, i)[1] for i in df.columns ] Output: >>> df $a $b $c $d $e 0 1 3 5 7 9 1 2 4 6 8 10 >>> df a b c d e 0 1 3 5 7 9 1 2 4 6 8 10
Note that the approaches in previous answers do not work for a MultiIndex. For a MultiIndex, you need to do something like the following: >>> df = pd.DataFrame({('$a','$x'):[1,2], ('$b','$y'): [3,4], ('e','f'):[5,6]}) >>> df $a $b e $x $y f 0 1 3 5 1 2 4 6 >>> rename = {('$a','$x'):('a','x'), ('$b','$y'):('b','y')} >>> df.columns = pandas.MultiIndex.from_tuples([ rename.get(item, item) for item in df.columns.tolist()]) >>> df a b e x y f 0 1 3 5 1 2 4 6
If you have to deal with loads of columns named by the providing system out of your control, I came up with the following approach that is a combination of a general approach and specific replacements in one go. First create a dictionary from the dataframe column names using regular expressions in order to throw away certain appendixes of column names and then add specific replacements to the dictionary to name core columns as expected later in the receiving database. This is then applied to the dataframe in one go. dict = dict(zip(df.columns, df.columns.str.replace('(:S$|:C1$|:L$|:D$|\.Serial:L$)', ''))) dict['brand_timeseries:C1'] = 'BTS' dict['respid:L'] = 'RespID' dict['country:C1'] = 'CountryID' dict['pim1:D'] = 'pim_actual' df.rename(columns=dict, inplace=True)
If you just want to remove the '$' sign then use the below code df.columns = pd.Series(df.columns.str.replace("$", ""))
In addition to the solution already provided, you can replace all the columns while you are reading the file. We can use names and header=0 to do that. First, we create a list of the names that we like to use as our column names: import pandas as pd ufo_cols = ['city', 'color reported', 'shape reported', 'state', 'time'] ufo.columns = ufo_cols ufo = pd.read_csv('link to the file you are using', names = ufo_cols, header = 0) In this case, all the column names will be replaced with the names you have in your list.
Here's a nifty little function I like to use to cut down on typing: def rename(data, oldnames, newname): if type(oldnames) == str: # Input can be a string or list of strings oldnames = [oldnames] # When renaming multiple columns newname = [newname] # Make sure you pass the corresponding list of new names i = 0 for name in oldnames: oldvar = [c for c in data.columns if name in c] if len(oldvar) == 0: raise ValueError("Sorry, couldn't find that column in the dataset") if len(oldvar) > 1: # Doesn't have to be an exact match print("Found multiple columns that matched " + str(name) + ": ") for c in oldvar: print(str(oldvar.index(c)) + ": " + str(c)) ind = input('Please enter the index of the column you would like to rename: ') oldvar = oldvar[int(ind)] if len(oldvar) == 1: oldvar = oldvar[0] data = data.rename(columns = {oldvar : newname[i]}) i += 1 return data Here is an example of how it works: In [2]: df = pd.DataFrame(np.random.randint(0, 10, size=(10, 4)), columns = ['col1', 'col2', 'omg', 'idk']) # First list = existing variables # Second list = new names for those variables In [3]: df = rename(df, ['col', 'omg'],['first', 'ohmy']) Found multiple columns that matched col: 0: col1 1: col2 Please enter the index of the column you would like to rename: 0 In [4]: df.columns Out[5]: Index(['first', 'col2', 'ohmy', 'idk'], dtype='object')
How to choose categorical variables?
I'm new to modeling I have a lot of functions and I have to separate them into discrete and continuous functions. There are only type tips on the internet Categorical = np.where(df.dtypes = np.object)[0] Categorical Then I have only categorical functions resulting from the format, and I mean real categorical functions. Is there a quick way to do this?
Whether a variable should be considered "discrete" or "continuous" is dependent on the variable and the use case. To count the number of distinct values a variable takes in your dataframe - you can use the pd.Series.nunique or pd.Series.value_counts functions and decide to treat a variable as discrete or continuous based on the output. pandas does come with a dedicated dtype called category which might be helpful - https://pandas.pydata.org/pandas-docs/stable/user_guide/categorical.html import pandas as pd df = pd.DataFrame({'A': [1, 1, 2, 3], 'B': ['a', 'a', 'b', 'b']}) In [3]: df Out[3]: A B 0 1 a 1 1 a 2 2 b 3 3 b In [4]: df.A.value_counts() Out[4]: 1 2 3 1 2 1 Name: A, dtype: int64 In [5]: df['B'].nunique() Out[5]: 2 In [6]: df['B'].unique() Out[6]: array(['a', 'b'], dtype=object)
And if you don't want to play in a loop, just use this simple line of code: categorical_fun = np.where((df2.dtypes == np.object)|(df2.nunique() <= 10))[0] categorical_fun
As you know that these categorical data are either in 'object' format or have a limited number of unique values, use a double condition and let the code show you these columns. Suppose you think that categorical variables have up to 10 unique values, then: a,b = df2.shape #<- ile mamy kolumn b print('ONLY DISCRETE FUNCTION') print('----------------------') for i in range(1,b): i = df2.columns[i] f = df2[i].dtypes h = df2[i].nunique() if f == np.object or h<=10: print(i,"---",f,"---",h)