ORDERING columns in sheet2 based on a column in sheet 1 - excel
By : user2589201
Date : March 29 2020, 07:55 AM
may help you . I am looking to order to the columns(B:I) in sheet 2 of an excel file based on the column order in sheet 1(A). Here is an example file , Use below formula : code :
=MATCH(A2,Sheet1!$A$2:$A$6,0)
|
How to match the ordering and sorting of multiple columns in Excel
By : Maria Pettersson
Date : March 29 2020, 07:55 AM
With these it helps Please see K1:N1 cells in the below graph. K1: =INDEX(A:A,MATCH($I1,$C:$C,0)) L1: =INDEX(B:B,MATCH($I1,$C:$C,0)) M1: =INDEX(E:E,MATCH($J1,$G:$G,0)) N1: =INDEX(F:F,MATCH($J1,$G:$G,0))
|
Renaming multiple columns in a panda dataframe where by renaming it you might accidently create columns with the same na
By : A.Corduff
Date : March 29 2020, 07:55 AM
Hope that helps Instead of renaming the column every time in the for loop. you could build a dict that maps old dates and new dates and then use that dict to rename. See the example below code :
old_date =[u'2016-Dec', u'2017-Jan', u'2017-Feb']
df = pd.DataFrame.from_records([(1,2,3)],columns = old_date)
print df
'''
2016-Dec 2017-Jan 2017-Feb
0 1 2 3
'''
new_date = [u'2017-Jan', u'2017-Feb', u'2017-Mar']
map_dict = {}
for o,n in zip(old_date,new_date):
map_dict[o] = n
print map_dict
'''
{u'2016-Dec': u'2017-Jan', u'2017-Jan': u'2017-Feb', u'2017-Feb': u'2017-Mar'}
'''
df.rename(columns=map_dict, inplace=True)
print df
'''
2017-Jan 2017-Feb 2017-Mar
0 1 2 3
'''
|
Parsing Excel data with pandas - why is it skipping columns when renaming columns?
By : Tobias
Date : March 29 2020, 07:55 AM
may help you . I really hope its something simple im missing. I'm reading in excel workbooks using python pandas. When I rename my columns to be numbers 1:len(columns) it skips the first few columns. code :
#dataframe have default columns names
df = pd.DataFrame({0:list('abcdef'),
1:[4,5,4,5,5,4],
2:[7,8,9,4,2,3]})
print (df)
0 1 2
0 a 4 7
1 b 5 8
2 c 4 9
3 d 5 4
4 e 5 2
5 f 4 3
#first column called index
print (df.index)
RangeIndex(start=0, stop=6, step=1)
#check columns names (RangeIndex can be also)
print (df.columns)
Int64Index([0, 1, 2], dtype='int64')
#add 1 to columns anmes and convert to str
df.columns = (df.columns + 1).astype(str)
print (df)
1 2 3
0 a 4 7
1 b 5 8
2 c 4 9
3 d 5 4
4 e 5 2
5 f 4 3
print (df.columns)
Index(['1', '2', '3'], dtype='object')
df = df.rename(columns = lambda x: str(x + 1))
print (df.columns)
Index(['1', '2', '3'], dtype='object')
df = pd.DataFrame({'a':list('abcdef'),
'f':[4,5,4,5,5,4],
'm':[7,8,9,4,2,3]})
print (df)
a f m
0 a 4 7
1 b 5 8
2 c 4 9
3 d 5 4
4 e 5 2
5 f 4 3
df.columns = pd.RangeIndex(1, df.shape[1] + 1).astype(str)
print (df.columns)
Index(['1', '2', '3'], dtype='object')
print (df)
1 2 3
0 a 4 7
1 b 5 8
2 c 4 9
3 d 5 4
4 e 5 2
5 f 4 3
|
Selecting, ordering and renaming columns in pandas
By : user3289029
Date : March 29 2020, 07:55 AM
it should still fix some issue There is no equivalent in pandas for select and rename in one method, is necesarry use similar like your solution: code :
df = df.rename(columns = {'treatment':'treat'})[['treat','score','obs']]
#alternative
#df = df[['treatment','score','obs']].rename(columns = {'treatment':'treat'})
print (df)
treat score obs
0 0 strong 1
1 1 weak 2
2 0 normal 3
3 1 weak 1
4 0 strong 2
|