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
  strange error from pandas dataframe djf123 1 290 Jul-27-2020, 05:25 AM
Last Post: scidam
  Pandas DataFrame not updating HelpMePlease 3 276 Jul-11-2020, 07:19 PM
Last Post: jefsummers
  Pandas DataFrame visual Truman 8 346 Jul-10-2020, 06:11 AM
Last Post: hussainmujtaba
  Pandas DataFrame and unmatched column sritsv19 0 282 Jul-07-2020, 12:52 PM
Last Post: sritsv19
  Pandas DataFrame Concatenate problems Kristenl2784 1 209 Jul-01-2020, 01:28 AM
Last Post: hussainmujtaba
  Difference of two columns in Pandas dataframe zinho 2 514 Jun-17-2020, 03:36 PM
Last Post: zinho
  iloc d8a988 7 403 Apr-27-2020, 10:15 AM
Last Post: d8a988
  error bars with dataframe and pandas Hucky 4 433 Apr-27-2020, 02:02 AM
Last Post: Hucky
  Python Pandas DataFrame Help AmericanEagle1989 1 275 Apr-12-2020, 12:37 PM
Last Post: AmericanEagle1989
  How does pyplot know what was plotted by the output of pandas.DataFrame(...).cumprod( codeowl 2 305 Mar-28-2020, 08:27 AM
Last Post: j.crater

Forum Jump:


Users browsing this thread: 1 Guest(s)