Rename first row in a CSV file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Rename first row in a CSV file (/thread-41304.html) |
Rename first row in a CSV file - James_S - Dec-17-2023 I don't know what I have done wrong. I want to rename the first row of the table: renamed_df = pd.read_csv('my_data.csv') # Change column names renamed_df.rename(columns={'AA': 'aaa', 'BB': 'bbb', 'CC': 'ccc', 'DD': 'ddd', 'EE': 'eee'}, inplace=True) # Save the renamed DataFrame to a new CSV file renamed_df.to_csv('renamed_my_data.csv', index=False) renamed_df.head() RE: Rename first row in a CSV file - James_S - Dec-17-2023 Input (my_data.csv): 39, AA, BB, CC, DD, EE 40, 111,222,333,444,555 41,666,777,888,999,000 42,121,232,343,454,565 43,244,355,366,377,388 Expected output (renamed_my_data.csv) 39, aaa, bbb, ccc, ddd, eee 40, 111,222,333,444,555 41,666,777,888,999,000 42,121,232,343,454,565 43,244,355,366,377,388 RE: Rename first row in a CSV file - deanhystad - Dec-17-2023 Your first column name is " AA", not "AA". You need to tell pandas that there may be extra whitespace df = pd.read_csv('my_data.csv', skipinitialspace=True) RE: Rename first row in a CSV file - James_S - Dec-17-2023 deanhystad, Thank you. It works. |