简体   繁体   中英

Pandas: Create dataframe from dataframe

I have a function that iterates over the rows of a DataFrame, and then creates a new DataFrame with it. The number of rows of the created DataFrame is the same, but the number and name of colums is not, so I can't

def _mod_df(df: pd.DataFrame) -> pd.DataFrame:
    dff = pd.DataFrame(COL_NAMES)
    for _, row in df.iterrows():
        col1 = row.colname8 / row.colname3
        col2 = row.colname2 ** 2
        col3 = row.colname[:8]

        dff = dff.concat([dff, Series([col1, col2, col3])], ignore_index=True)

    return dff

Is there any way to optimize this?

Try:

def _mod_df(df: pd.DataFrame) -> pd.DataFrame:
    return pd.DataFrame([(df.colname8 / df.colname3).values,
                         (df.colname2 ** 2).values,
                         df.colname[:8].values],
                        columns=COL_NAMES)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM