Создание фреймов данных из функций в Python, R и Julia: примеры и код

Чтобы создать DataFrame из функции, вы можете использовать различные методы на разных языках программирования. Вот несколько примеров на Python, R и Julia:

Python:

import pandas as pd
def create_dataframe():
    data = {'Name': ['John', 'Emma', 'Alex'],
            'Age': [25, 30, 35],
            'City': ['New York', 'San Francisco', 'London']}
    df = pd.DataFrame(data)
    return df
# Call the function to create the DataFrame
df = create_dataframe()
print(df)

Р:

create_dataframe <- function() {
  data <- data.frame(Name = c('John', 'Emma', 'Alex'),
                     Age = c(25, 30, 35),
                     City = c('New York', 'San Francisco', 'London'))
  return(data)
}
# Call the function to create the DataFrame
df <- create_dataframe()
print(df)

Юля:

using DataFrames
function create_dataframe()
    data = DataFrame(Name = ["John", "Emma", "Alex"],
                     Age = [25, 30, 35],
                     City = ["New York", "San Francisco", "London"])
    return data
end
# Call the function to create the DataFrame
df = create_dataframe()
println(df)

Во всех трех примерах мы определяем функцию под названием create_dataframe, которая возвращает DataFrame, созданный на основе словаря, фрейма данных или конструктора DataFrame соответственно. Вызвав функцию, мы создаем DataFrame и печатаем его.