logo
down
shadow

Excel - VB - Re-ordering /Renaming columns


Excel - VB - Re-ordering /Renaming columns

By : Weranga
Date : December 01 2020, 04:55 PM
will help you I have found that converting the data on the sheet to a table makes it a lot better to program for and a lot less messy. If you convert your data to a table with the name 'TableName' and wish to rename a certain column # (2 for this example) to 'Column Title' this would be the code.
code :


Share : facebook icon twitter icon
ORDERING columns in sheet2 based on a column in sheet 1 - excel

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

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

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?

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

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
Related Posts Related Posts :
  • Wrong data being highlighted in conditional formatting
  • "Overload" F9 (or shift+F9) in Excel spreadsheet with VBA function?
  • ShowAllData method of Worksheet class failed
  • VBA If cell is under a certain length, highlight and display message
  • VBA: Unable to get the Match property of the WorkSheet function class
  • Highlighting the second and more duplicate rows in excel but not first occurrence?
  • Excel add- in focus not returned to the active sheet
  • How to restrict VBA code to a specific worksheet?
  • How to insert column name in the destination table in ssis?
  • Insert text into the background of a cell
  • how do I generate random timestamps in Excel
  • VBScript Opening folder to convert csv to xls
  • Creating a VBA Refresh Macro in Smart View for Oracle
  • Excel Performance issues with countif on multiple rows
  • excel vba http request download data from yahoo finance
  • Excel Vba Loop , not running properly
  • Efficiently hiding / unhiding many (+500) rows in Excel VBA
  • Excel's Find function; Exceptions over values
  • reading data from an excel sheet in perl
  • Using Webservice function in Excel to pull "Next Earnings Date" from Yahoo Finance using Yahoo API
  • Three And (&)'s into an if statement VBA for Excel
  • Certain fractions being calculated in excel 2013
  • Excel formula to find sum of difference
  • countfis or sum if array to work out if given time is between the value of two columns
  • Error in Date Cell in CSV
  • Syntax while trying to include The multi-part identifier [°C] in command text
  • How to find day of the week from known date and month but variable year
  • Modify Marker Transparency with Excel VBA
  • Excel tricky formula
  • Loop Though All UDF Names in Project
  • Searching for function usage in Excel VBA
  • Convert from list to flat CSV with modified data
  • Count how many words from a list appear in a cell
  • Insert Missing Years between 2 years
  • Creating hierarchical sequence in Excel using several functions in formula
  • On Error GoTo statement is still executing although there is no error generated
  • Find last digits with IF
  • Use Excel VBA to find column matches & merge based on the values of two other column
  • PowerPivot not sending Authorization header in Basic Authentication to OData Svc
  • Vlookup not working with text search
  • Paste value from a Userform to a Excel Sheet
  • Excel 2010 Macro to compare two columns for finding the matched value
  • #VALUE error when dealing with long string in UDF in VBA(excel)
  • Refer to a cell in another worksheet by referencing the current sheet's name
  • VBA Excel 2007 : Need to loop copy and loop count number except zero every row above
  • Excel: Conditional probabilities of winning a tiebreaker game
  • How to find groups of individuals who attended multiple events together?
  • Using DateTime when referencing a cell with SUMIFS()
  • VBA Copy & Transpose Data Range
  • Huge excel file... any advice?
  • Looped macro to create a new sheet, rename it, add data from the web, then loop back through until complete
  • copy paste of tab delimited text results in strange formatting in Excel?
  • Excel user defined function: Nth_Occurence
  • Excel ActiveX Button is clickable, but does not work
  • Array Formula Conditional Count
  • YEAR() fitting to WEEKNUM(...,21) in Excel
  • Macro that follows a link and downloads the table into a new sheet
  • How to Concatenate multiple columns if not empty
  • Excel - List all the combinations of two words from one list, in two other columns
  • How do I do my Pentaho Report take the Excel datasource file along to BA/BI Server?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org