如何从 for 循环构建和填充 pandas 数据框?
How to build and fill pandas dataframe from for loop?
这是我 运行 的代码的一个简单示例,我希望将结果放入 pandas 数据帧(除非有更好的选择):
for p in game.players.passing():
print p, p.team, p.passing_att, p.passer_rating()
R.Wilson SEA 29 55.7
J.Ryan SEA 1 158.3
A.Rodgers GB 34 55.8
使用此代码:
d = []
for p in game.players.passing():
d = [{'Player': p, 'Team': p.team, 'Passer Rating':
p.passer_rating()}]
pd.DataFrame(d)
我可以得到:
Passer Rating Player Team
0 55.8 A.Rodgers GB
这是一个 1x3 数据框,我理解为什么它只有一行,但我不知道如何使它成为多行且列正确命令。理想情况下,该解决方案将能够处理 n 行数(基于 p),如果列数由统计要求。有什么建议么?提前致谢!
使用列表理解试试这个:
import pandas as pd
df = pd.DataFrame(
[p, p.team, p.passing_att, p.passer_rating()] for p in game.players.passing()
)
最简单的答案就是 Paul H 所说的:
d = []
for p in game.players.passing():
d.append(
{
'Player': p,
'Team': p.team,
'Passer Rating': p.passer_rating()
}
)
pd.DataFrame(d)
但如果你真的想 "build and fill a dataframe from a loop",(顺便说一句,我不推荐),你可以这样做。
d = pd.DataFrame()
for p in game.players.passing():
temp = pd.DataFrame(
{
'Player': p,
'Team': p.team,
'Passer Rating': p.passer_rating()
}
)
d = pd.concat([d, temp])
用你的数据创建一个元组列表,然后用它创建一个 DataFrame:
d = []
for p in game.players.passing():
d.append((p, p.team, p.passer_rating()))
pd.DataFrame(d, columns=('Player', 'Team', 'Passer Rating'))
元组列表的开销应该小于列表字典。我在下面对此进行了测试,但请记住在大多数情况下优先考虑代码理解的难易程度而不是性能。
测试函数:
def with_tuples(loop_size=1e5):
res = []
for x in range(int(loop_size)):
res.append((x-1, x, x+1))
return pd.DataFrame(res, columns=("a", "b", "c"))
def with_dict(loop_size=1e5):
res = []
for x in range(int(loop_size)):
res.append({"a":x-1, "b":x, "c":x+1})
return pd.DataFrame(res)
结果:
%timeit -n 10 with_tuples()
# 10 loops, best of 3: 55.2 ms per loop
%timeit -n 10 with_dict()
# 10 loops, best of 3: 130 ms per loop
我可能错了,但我认为@amit 接受的答案有一个错误。
from pandas import DataFrame as df
x = [1,2,3]
y = [7,8,9,10]
# this gives me a syntax error at 'for' (Python 3.7)
d1 = df[[a, "A", b, "B"] for a in x for b in y]
# this works
d2 = df([a, "A", b, "B"] for a in x for b in y)
# and if you want to add the column names on the fly
# note the additional parentheses
d3 = df(([a, "A", b, "B"] for a in x for b in y), columns = ("l","m","n","o"))
这是我 运行 的代码的一个简单示例,我希望将结果放入 pandas 数据帧(除非有更好的选择):
for p in game.players.passing():
print p, p.team, p.passing_att, p.passer_rating()
R.Wilson SEA 29 55.7
J.Ryan SEA 1 158.3
A.Rodgers GB 34 55.8
使用此代码:
d = []
for p in game.players.passing():
d = [{'Player': p, 'Team': p.team, 'Passer Rating':
p.passer_rating()}]
pd.DataFrame(d)
我可以得到:
Passer Rating Player Team
0 55.8 A.Rodgers GB
这是一个 1x3 数据框,我理解为什么它只有一行,但我不知道如何使它成为多行且列正确命令。理想情况下,该解决方案将能够处理 n 行数(基于 p),如果列数由统计要求。有什么建议么?提前致谢!
使用列表理解试试这个:
import pandas as pd
df = pd.DataFrame(
[p, p.team, p.passing_att, p.passer_rating()] for p in game.players.passing()
)
最简单的答案就是 Paul H 所说的:
d = []
for p in game.players.passing():
d.append(
{
'Player': p,
'Team': p.team,
'Passer Rating': p.passer_rating()
}
)
pd.DataFrame(d)
但如果你真的想 "build and fill a dataframe from a loop",(顺便说一句,我不推荐),你可以这样做。
d = pd.DataFrame()
for p in game.players.passing():
temp = pd.DataFrame(
{
'Player': p,
'Team': p.team,
'Passer Rating': p.passer_rating()
}
)
d = pd.concat([d, temp])
用你的数据创建一个元组列表,然后用它创建一个 DataFrame:
d = []
for p in game.players.passing():
d.append((p, p.team, p.passer_rating()))
pd.DataFrame(d, columns=('Player', 'Team', 'Passer Rating'))
元组列表的开销应该小于列表字典。我在下面对此进行了测试,但请记住在大多数情况下优先考虑代码理解的难易程度而不是性能。
测试函数:
def with_tuples(loop_size=1e5):
res = []
for x in range(int(loop_size)):
res.append((x-1, x, x+1))
return pd.DataFrame(res, columns=("a", "b", "c"))
def with_dict(loop_size=1e5):
res = []
for x in range(int(loop_size)):
res.append({"a":x-1, "b":x, "c":x+1})
return pd.DataFrame(res)
结果:
%timeit -n 10 with_tuples()
# 10 loops, best of 3: 55.2 ms per loop
%timeit -n 10 with_dict()
# 10 loops, best of 3: 130 ms per loop
我可能错了,但我认为@amit 接受的答案有一个错误。
from pandas import DataFrame as df
x = [1,2,3]
y = [7,8,9,10]
# this gives me a syntax error at 'for' (Python 3.7)
d1 = df[[a, "A", b, "B"] for a in x for b in y]
# this works
d2 = df([a, "A", b, "B"] for a in x for b in y)
# and if you want to add the column names on the fly
# note the additional parentheses
d3 = df(([a, "A", b, "B"] for a in x for b in y), columns = ("l","m","n","o"))