0

I have an existing out with columns name:

out = ["nodeBLocalCellId_42", "configuredMaxTxPower_3G_43", "latitude_44", "nodeBLocalCellId_46", "configuredMaxTxPower_3G_47", "latitude_48", .....] 

out_values: [A3, 4000, 5278843, A0, 4000, 5278843]

now I want to create a new df out2 with columns name:

out2 = ["nodeBLocalCellId_1", "configuredMaxTxPower_3G_1", "latitude_1", "nodeBLocalCellId_2", "configuredMaxTxPower_3G_2", "latitude_2", ...]  

Now I want to copy the columns data from out to out2 by using for loop and if condition as below: I am not getting the proper output, I am exporting out2_to_excel, my excel sheet is empty

    import pandas as pd
    df = pd.DataFrame({'nodeBLocalCellId_42': ['A3', 'A0', 'B3'], 
                   'configuredMaxTxPower_3G_43': [40000, 50000, 42000],
                   'latitude_44': [40000, 50000, 42000],
                   'nodeBLocalCellId_46': [40000, 50000, 42000],
                   'configuredMaxTxPower_3G_47': [40000, 50000, 42000],
                   'latitude_48': [40000, 50000, 42000]
                  
                  
                  })
                  
    out2 = ["nodeBLocalCellId_1", "configuredMaxTxPower_3G_1", "latitude_1", "nodeBLocalCellId_2", "configuredMaxTxPower_3G_2", "latitude_2"]


    out = pd.DataFrame(columns=column_names)
    out2 = pd.DataFrame(columns=column_names_out2

    mappings = {

        "configuredMaxTxPower_3G": {
            "i": "1",
            "j": "2"
        },


        "latitude": {
            "i": "1",
            "j": "2"

        "nodeBLocalCellId": {
            "i": "1",
            "j": "2"
        },

    }


                    
                    

    for column in out.columns:
        for mapping_key, mapping_values in mappings.items():
            if f"{mapping_key}{mapping_values['i']}" in column:
                i = mapping_values["i"]
                j = mapping_values["j"]
                out2[column.replace(f"{mapping_key}{i}", f"{mapping_key}{j}")] = out[column]           

    out2.to_excel('/path.xlsx')        
Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
  • please provide a minimal reproducible example of the **dataset** (with actual values) and the matching expected output. You might want to limit it to 3/4 columns, <10 rows for clarity – mozway Jul 12 '23 at 11:35
  • out= ["nodeBLocalCellId_42", "configuredMaxTxPower_3G_43", "latitude_44", "nodeBLocalCellId_46", "configuredMaxTxPower_3G_47", "latitude_48"] and out_values: [A3, 4000, 5278843, A0, 4000, 5278843], new df: out2 = ["nodeBLocalCellId_1", "configuredMaxTxPower_3G_1", "latitude_1", "nodeBLocalCellId_2", "configuredMaxTxPower_3G_2",latitude_2"], now I want to copy the data from out to out2 using for loop and if statement like: like copy out[nodeBLocalCellId_42] to out2[nodeBLocalCellId_1], [nodeBLocalCellId_46] to out2[nodeBLocalCellId_2] similarly for all columns – Prashant Sharma Jul 12 '23 at 11:47
  • please [edit] the question with a [DataFrame constructor for each input](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples), not just the column names. – mozway Jul 12 '23 at 11:50
  • @mozway I have edited the question now with short data. but actually I have a large number of columns in my input sheet – Prashant Sharma Jul 12 '23 at 11:56

0 Answers0