Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 pandas dataframe iloc mystery
#1
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:

Output:
columns a a a b b b indexes 0.0 0.0 0.0 0.0 0.0 0.0 0.0 a 0.0 0.0 0.0 0.0 0.0 0.0 0.0 a 0.0 0.0 0.0 0.0 0.0 0.0 0.0 a 0.0 0.0 0.0 0.0 0.0 0.0 0.0 b 0.0 0.0 0.0 0.0 0.0 0.0 0.0 b 0.0 0.0 0.0 0.0 0.0 0.0 0.0 b 0.0 0.0 0.0 0.0 0.0 0.0 0.0 columns a a a b b b indexes 0 0.0 0.0 0.0 0.0 0.0 0.0 a a_1 0.0 0.0 0.0 0.0 0.0 0.0 a a_2 0.0 0.0 0.0 0.0 0.0 0.0 a a_3 0.0 0.0 0.0 0.0 0.0 0.0 b b_4 0.0 0.0 0.0 0.0 0.0 0.0 b b_5 0.0 0.0 0.0 0.0 0.0 0.0 b b_6 0.0 0.0 0.0 0.0 0.0 0.0 columns a a a b b b indexes 0 a_3 a_3 a_3 b_6 b_6 b_6 a a_1 0 0 0 0 0 0 a a_2 0 0 0 0 0 0 a a_3 0 0 0 0 0 0 b b_4 0 0 0 0 0 0 b b_5 0 0 0 0 0 0 b b_6 0 0 0 0 0 0
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!
Quote
#2
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.
Quote
#3
I just tested and that fixed it. Moral of the story - do not duplicate names of columns.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Obtaining Correct Date In Pandas DataFrame eddywinch82 7 386 Yesterday, 04:47 PM
Last Post: sandeep_ganga
  Parse XML String in Pandas Dataframe creedX 2 282 Dec-09-2019, 07:35 PM
Last Post: creedX
  Pandas dataframe to join three tables using like condition among them sandeep_ganga 0 237 Nov-29-2019, 08:30 AM
Last Post: sandeep_ganga
  Pandas Dataframe to Google Big Query Ecniv 2 883 Nov-21-2019, 02:26 PM
Last Post: Ecniv
  manipulating a dataframe - pandas nsx200 2 217 Nov-14-2019, 10:38 AM
Last Post: nsx200
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 218 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  How to add a few empty rows into a pandas dataframe python_newbie09 2 1,251 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Dropping a column from pandas dataframe marco_ita 6 1,639 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  created a pandas series instead of pandas DataFrame ibaad1406 6 778 Sep-06-2019, 06:23 AM
Last Post: ibaad1406
  Applying operation to a pandas multi index dataframe subgroup Nuovoq 1 472 Sep-04-2019, 10:04 PM
Last Post: Nuovoq

Forum Jump:


Users browsing this thread: 2 Guest(s)