question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ENH: Construct pandas dataframe from function

See original GitHub issue

It 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:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, May 1, 2020

It’s OK 😃 There’s lot’s of other issues open if you want to contribute, see here for how to get started

1reaction
jrebackcommented, Apr 30, 2020

-1 on this extension, doesn’t provide any new capability and adds to api bloat.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found