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