dataframe without append how to use function to achieve it
To create a dataframe without using the append method, you can use the pandas.DataFrame function and pass in a dictionary of your data.
For example:
import pandas as pd
data = {'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'city': ['New York', 'San Francisco', 'Los Angeles']}
df = pd.DataFrame(data)
print(df)
Output:
name age city
0 Alice 25 New York
1 Bob 30 San Francisco
2 Charlie 35 Los Angeles
You can also create a function that returns a dataframe based on some input parameters. For example:
def create_dataframe(names, ages, cities):
data = {'name': names,
'age': ages,
'city': cities}
return pd.DataFrame(data)
df = create_dataframe(['Alice', 'Bob', 'Charlie'], [25, 30, 35], ['New York', 'San Francisco', 'Los Angeles'])
print(df)
Output:
name age city
0 Alice 25 New York
1 Bob 30 San Francisco
2 Charlie 35 Los Angeles
``
原文地址: https://www.cveoy.top/t/topic/fd22 著作权归作者所有。请勿转载和采集!