我在代码中使用df.append()在数据帧列之间附加百分比变化。由于df.append()在 pandas 1.4 中被折旧,我尝试使用pd.concat但无法复制输出。
这就是我现在所拥有的:
import numpy as np
import pandas as pd
df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo",
"bar", "bar", "bar", "bar"],
"B": ["one", "one", "one", "two", "two",
"one", "one", "two", "two"],
"C": ["small", "large", "large", "small",
"small", "large", "small", "small",
"large"],
"D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
"E": [2, 4, 5, 5, 6, 6, 8, 9, 9]})
table = pd.pivot_table(df, values='D', index=['A', 'B'],
columns=['C'], aggfunc=np.sum, fill_value=0, …Run Code Online (Sandbox Code Playgroud)