ENH: Construct pandas dataframe from function
See original GitHub issueIt would be great if we could construct a pd.DataFrame
from a function.
Describe the solution you’d like
import pandas as pd
import numpy as np
def my_random():
return np.random.rand() + 1
df = pd.DataFrame(my_random, index=range(10), columns=range(15))
The output would be equivalent to
col_len = 10
index_len = 15
data = np.array([[my_random() for i in range(col_len)] for j in range(index_len)])
df = pd.DataFrame(data, index=range(index_len), columns=range(columns_len))
Alternatively, we could do the same with something like
df = pd.DataFrame.from_function(my_random, *args, **kwargs)
args
and kwargs
are passed to the df constructor.
EDIT
I just realized I can easily obtain what I want with
df = pd.DataFrame(index=range(10), columns=range(15)).applymap(
lambda x: my_random()
)
It’s quite compact and easy to read. It’s slowish but I don’t expect it to be used in high performance tasks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (6 by maintainers)
Top Results From Across the Web
pandas.DataFrame — pandas 1.5.2 documentation
Construct DataFrame from dict of array-like or dicts. Convert structured or record ndarray to DataFrame. Get Greater than or equal to of dataframe...
Read more >Different ways to create Pandas Dataframe - GeeksforGeeks
DataFrame() function is used to create a dataframe in Pandas. The syntax of creating dataframe is: pandas.DataFrame(data, index, columns).
Read more >Creating Pandas DataFrame by using Numpy arrays. - Plus2net
We will create DataFrame by using 1-D and 2-D Numpy arrays (numpy ndarray). DataFrame can be created by using Numpy arrays. We know...
Read more >WORKSHEET – Data Handling Using Pandas
Write a statement to create DataFrame called df. Assume that pandas has been imported as pd. df=pd.DataFrame(Smarks,index=[1,2,3]).
Read more >pandas remove words from string - Coffeenote
Remove ends of string entries in pandas DataFrame column. ... Using the replace() function to Make Replacements in Strings in Python Example 2:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It’s OK 😃 There’s lot’s of other issues open if you want to contribute, see here for how to get started
-1 on this extension, doesn’t provide any new capability and adds to api bloat.