簡體   English   中英

如何創建 pandas dataframe 並從 function 填充它?

[英]How to create pandas dataframe and fill it from function?

我有那個 function:

def count (a,b):
    x = a*b

'a' 和 'b' 的值對於 'a' 必須是 1...99,對於 'b' 必須是 100...800。 所以問題是如何創建 pandas dataframe 垂直的 a 值和水平的 b 值和內部的 x 值用“計數”ZC1C425268E68385D14AB5074C17ZA 和 b 的所有組合計算? 它必須看起來像這樣:示例

這可以通過矩陣乘法輕松完成:

import pandas as pd
import numpy as np

a = np.arange(1,100)
b = np.arange(100,801)

df = pd.DataFrame(np.matmul(a.reshape(-1,1),b.reshape(1,-1), index=a, columns=b)

希望這可能會有所幫助

import pandas as pd

def count(a,b):
    x = a*b
    return x

a = list(range(1,100))
b = list(range(100,801))
data = []
for i in a:
    temp = [i]
    for j in b:
        temp.append(count(i,j))
    data.append(temp)

df = pd.DataFrame(data, columns=["a/b"]+b)
# to save as csv
df.to_csv("data.csv", index=False)

In this case where your function count is vectorizable, you can use Numpy's fromfunction with a little help from a lambda function to convert the grid co-ordinate indices arrays into your a, b values:

import numpy as np
import pandas as pd

def count(a, b):
    return a*b

a = list(range(1,100))
b = list(range(100,801))

data = np.fromfunction(
    lambda ii, jj: count(np.array(a)[ii], np.array(b)[jj]),
    shape=(len(a), len(b)), 
    dtype='int'
)
df1 = pd.DataFrame(data, index=a, columns=b)
print(df1.iloc[:5, :5])

   100  101  102  103  104
1  100  101  102  103  104
2  200  202  204  206  208
3  300  303  306  309  312
4  400  404  408  412  416
5  500  505  510  515  520

這需要大約 731 µs ± 12.6 µs 的執行時間。

我能想到的唯一其他解決方案是:

df2 = pd.DataFrame(None, index=a, columns=b)
ii, jj = np.meshgrid(a, b)
for i, j in zip(ii.ravel(), jj.ravel()):
    df2.loc[i, j] = count(i, j)  # this is slow
assert(np.array_equal(df1, df2))

這大約需要 4.17 秒 ± 62.7 毫秒。

df3 = pd.DataFrame(None, index=a, columns=b)
df3 = df3.apply(lambda col: [count(i, col.name) for i in col.index])
assert(np.array_equal(df1, df3))

這大約需要 97.3 毫秒 ± 1.96 毫秒。

我實際上認為如果有一個 Pandas function 會很棒,比如pd.from_function(count, index=a, columns=b)也許。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM