我是python中的新手。我有一个数据框,我将它写入excel。我要插入一个带子串的空行" Total"列"放置ID"值会更改并获取其他列的字段值的总和。
目前看来这样的数据。
希望以这种方式转换它。
数据在这里
https://drive.google.com/open?id=15gef0zEReQb7qA7TsJSxIhYxLVVJLK4X
提前致谢。
答案 0 :(得分:1)
这看起来怎么样?
# import pandas
import pandas as pd
# import data
df = pd.read_excel(r"C:\Users\IT045886\Downloads\Data.xlsx", skiprows = 12)
# convert str(floats) to actual floats
for col in df.loc[:, 'KM_Impressions':].columns:
df[col] = pd.to_numeric(df[col].copy(), errors = 'coerce')
# limit column selection
df1 = df.loc[:, :'Deep Engagements'].copy()
# drop NaN and pivot
subs = df1.dropna().pivot_table(index = [
'Placement ID',
'Placement#',
'Metric',
'Day'
], aggfunc = 'sum')
# sum on first three levels
subs_t = subs.sum(level = [0,1,2])
# insert 'Total' in 'Day' column
subs_t.insert(0, 'Day', 'Total')
# set 'Day' in index
subs_t.set_index('Day', append = True, inplace = True)
# concat sums and totals
df2 = pd.concat([subs, subs_t]).sort_index().reset_index()
# merge original columns back in, reorder columns
final = df2.merge(df, how = 'outer')[df.columns]
final
pd.to_numeric()
的文档 - > https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html
errors = 'coerce' # invalid parsing will be set as NaN