[英]How to store results from for-loop into dataframe columns (Python 3)
I am new to Python and trying to store results from a for-loop into dataframe columns (Python 3).我是 Python 的新手,并试图将 for 循环的结果存储到 dataframe 列(Python 3)中。 Let's say I have the following data:假设我有以下数据:
time=[1,2,3,4]
i=[1,2,3,4,5]
j=[10,20,30,40,50]
k=[100,200,300,400,500]
data_ijk=list(zip(i,j,k))
[(1, 10, 100), (2, 20, 200), (3, 30, 300), (4, 40, 400), (5, 50, 500)]
I want to loop through the data_ijk to solve the following equation:我想遍历 data_ijk 来求解以下等式:
eq=(i+j+k)*t
and put the results of equation into dataframe with column labels col_labels=["A","B","C","D","E"]
for each set of ijk and rows considering time (t).并将方程的结果放入 dataframe 中,对于每组 ijk 和考虑时间 (t) 的行,列标签col_labels=["A","B","C","D","E"]
。
The expected output should look something like this:预期的 output 应如下所示:
t吨 | A一种 | B乙 | C C | D丁 | E乙 |
---|---|---|---|---|---|
1 1个 | 111 111 | 222 222 | 333 333 | 444 444 | 555 555 |
2 2个 | 222 222 | 444 444 | 666 666 | 888 888 | 1100 1100 |
3 3个 | 333 333 | 666 666 | 999 999 | 1332 1332 | 1665 1665 |
4 4个 | 444 444 | 888 888 | 1332 1332 | 1776 1776 | 2220 2220 |
I know i need to define dataframe我知道我需要定义 dataframe
df = pd.DataFrame(columns=[col_labels])
and I assume I should create nested for loop with data_abc and time, but I have no idea how to define to put results in different columns我假设我应该用 data_abc 和时间创建嵌套的 for 循环,但我不知道如何定义将结果放在不同的列中
for i,j,k in data_abc:
for t in time:
...???
print((i+j+k)*t)
Try:尝试:
df = pd.DataFrame(data=[[sum(x)*t for x in zip(i,j,k)] for t in time],
columns=list("ABCDE"),
index=time)
>>> df
A B C D E
1 111 222 333 444 555
2 222 444 666 888 1110
3 333 666 999 1332 1665
4 444 888 1332 1776 2220
Try:尝试:
import pandas as pd
# In case, the column names are actually words than single letters
col_labels=["A","B","C","D","E"]
time=[1,2,3,4]
data = dict(i=[1,2,3,4,5],
j=[10,20,30,40,50],
k=[100,200,300,400,500])
df = pd.DataFrame(data)
cons_sum_ijk = df.apply(sum, axis=1).to_list()
df = pd.DataFrame(columns=col_labels, data=[cons_sum_ijk]* len(time) )
df['t'] = time
df[col_labels] = df.apply(lambda x: x[col_labels]*x['t'], axis=1)
df = df[['t'] + col_labels]
>>> df
t A B C D E
0 1 111 222 333 444 555
1 2 222 444 666 888 1110
2 3 333 666 999 1332 1665
3 4 444 888 1332 1776 2220
Using Numpy's einsum
使用 Numpy 的einsum
import numpy as np
import pandas as pd
time = [1, 2, 3, 4]
i = [1, 2, 3, 4, 5]
j = [10, 20, 30, 40, 50]
k = [100, 200, 300, 400, 500]
df = pd.DataFrame(np.einsum('a,bc->ac', time, np.array([i, j, k])), columns=[*'ABCDE'])
df
A B C D E
0 111 222 333 444 555
1 222 444 666 888 1110
2 333 666 999 1332 1665
3 444 888 1332 1776 2220
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.