How to add a column value to a dictionary framework using a for loop so that each dataframe gets a unique column?

I want to add codesin dataframe dictionary.

 codes = [['01', '02', '03', '05', '06', '08', '10', '11', '13', '15', '17', '19', '21', '23', '25', '27', '29', '31', '33', '35', '37', '39', '43', '45', '4.55', '48', '52']
 #27Codes

 df = pd.read_excel(sales,sheet_name=None,ignore_index = True, skiprows=7)
 #27 Sheets
 for i in codes:
      for key in df.keys():
          df['Sheet1']['Code'] = i

I cannot understand why I seem to have idata in every frame. I think I understand why I cannot figure out how to fix this.

I am new to coding and I generally cannot find a solution.

Expected Result:

df['Sheet1']

   Date         Particulars    Inwards  Code

1 2017-04-01         EFG           12800    01
2 2017-07-22         ABC           100      01
3 2017-09-05         BCD           10000    01
4 2018-03-13         ABC           2000     01
Column

Codeshould be 02in the next data frame and so on.

After that, I want concatdata and data group_by, and then write to excel.

TIA

+4
source share
2 answers

pandas.read_excel DataFrames, .

, DataFrame .

:

for i in codes:
    for key in df.keys():
        df['Sheet1']['Code'] = i

. -, key. "Sheet1". -, , , .

. :

for i in range(len(codes)):
    code = codes[i]
    key = df.keys()[i]
    df[key]['Code'] = code

, zip(). , , :

for code, key in zip(codes, df.keys()):
    df[key]['Code'] = code

, codes df.

DataFrames, pandas.concat:

combined = pd.concat(df)

, concat:

, DataFrame

...

dict, , > , (. ).

+3

:

df = {k: v.assign(Code=x) for x, (k, v) in zip(codes, df.items())}

pd.DataFrame.assign .

+4

Source: https://habr.com/ru/post/1695878/


All Articles