2

This question is related to many previously asked about adding columns to a dataframe, but I could not find one that addresses my problem.

I have 2 lists and I want to create a dataframe for them where each list is a column, and the index is taken from a previous dataframe.

When I try:


STNAME = Filter3['STNAME'].tolist() #first list to be converted to column
CTYNAME = Filter3['CTYNAME'].tolist() #second list to be converted to column
ORIG_INDEX = Filter3.index #index pulled from previous dataframe

FINAL = pd.Series(STNAME, CTYNAME, index=ORIG_INDEX)

return FINAL

I get an error that an index already exists: TypeError: init() got multiple values for argument 'index'

So I tried it with just two columns, and no declaration of index, and it turns out that

FINAL = pd.Series(STNAME, CTYNAME) makes the CTYNAME into the index:


STNAME = Filter3['STNAME'].tolist()
CTYNAME = Filter3['CTYNAME'].tolist()
ORIG_INDEX = Filter3.index
FINAL = pd.Series(STNAME, CTYNAME)

return FINAL

Washington County Iowa

Washington County Minnesota

Washington County Pennsylvania

Washington County Rhode Island

Washington County Wisconsin

dtype: object


How would I create a dataframe that accepts 2 lists as columns and a third index (with matching length) as the index?

Thank you very much

ZakS
  • 1,073
  • 3
  • 15
  • 27

1 Answers1

3

I believe need DataFrame, not Series if want working with lists:

FINAL = pd.DataFrame({'STNAME':STNAME, 'CTYNAME': CTYNAME}, 
                     index=ORIG_INDEX,
                     columns = ['STNAME', 'CTYNAME'])

Or better is create only subset by list of columns and for avoid possible SettingWithCopyWarning add DataFrame.copy:

FINAL = Filter3[['STNAME', 'CTYNAME']].copy()

Sample:

d = {'COL': ['a', 'b', 's', 'b', 'b'], 
    'STNAME': ['Iowa', 'Minnesota', 'Pennsylvania', 'Rhode Island', 'Wisconsin'], 
    'CTYNAME': ['Washington County', 'Washington County', 'Washington County', 
                 'Washington County', 'Washington County'],}

Filter3 = pd.DataFrame(d,index=[10,20,3,50,40])
print (Filter3)
   COL            CTYNAME        STNAME
10   a  Washington County          Iowa
20   b  Washington County     Minnesota
3    s  Washington County  Pennsylvania
50   b  Washington County  Rhode Island
40   b  Washington County     Wisconsin

FINAL = Filter3[['STNAME', 'CTYNAME']].copy()
print (FINAL)
          STNAME            CTYNAME
10          Iowa  Washington County
20     Minnesota  Washington County
3   Pennsylvania  Washington County
50  Rhode Island  Washington County
40     Wisconsin  Washington County
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252