pandas dataframe iloc mystery - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: pandas dataframe iloc mystery (/thread-21912.html) |
pandas dataframe iloc mystery - edvvardbrian - Oct-20-2019 Hello! So I have a pandas dataframe filled with zeros, and I want to set the very first column and the very first row to the following values: ["a_1","a_2","a_3","b_4","b_5","b_6"]. I'm using this code: import numpy as np import pandas as pd data_array = np.zeros([7, 7]) data_df = pd.DataFrame(data_array, index=["indexes","a","a","a","b","b","b"], columns=["columns","a","a","a","b","b","b"]) print(data_df, "\n") data_df.iloc[:,0] = [0] + ["a_1","a_2","a_3","b_4","b_5","b_6"] print(data_df, "\n") data_df.iloc[0,:] = [0] + ["a_1","a_2","a_3","b_4","b_5","b_6"] print(data_df, "\n")However, this does not work, because the final output is: As you can see in the last table, the first column is set to the right values (a_1 a_2 a_3 b_4 b_5 b_6), but the first row is set instead to (a_3 a_3 a_3 b_6 b_6 b_6).For the life of me I can't figure out why this happens. Thanks! RE: pandas dataframe iloc mystery - jefsummers - Oct-28-2019 What if you change the names of the columns to 6 different letters? Thinking about what goes on under the hood with keys and lookups I can see Pandas getting confused by identically named columns. RE: pandas dataframe iloc mystery - jefsummers - Oct-29-2019 I just tested and that fixed it. Moral of the story - do not duplicate names of columns. |