Python Forum
pandas dataframe iloc mystery
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!
Reply
#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.
Reply
#3
I just tested and that fixed it. Moral of the story - do not duplicate names of columns.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [Solved] Formatting cells of a pandas dataframe into an OpenDocument ods spreadsheet Calab 1 650 Mar-01-2025, 04:51 AM
Last Post: Calab
  Find duplicates in a pandas dataframe list column on other rows Calab 2 2,134 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,610 Aug-26-2024, 04:52 PM
Last Post: Calab
  Add NER output to pandas dataframe dg3000 0 1,144 Apr-22-2024, 08:14 PM
Last Post: dg3000
  HTML Decoder pandas dataframe column mbrown009 3 2,623 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,987 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 2,063 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 3,390 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,896 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 2,599 Jan-10-2022, 07:19 PM
Last Post: moduki1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020