- 使用字典构建
import pandas as pd
data = {
'Column1': [1, 2, 3],
'Column2': ['A', 'B', 'C']
}
df = pd.DataFrame(data)
- 使用列表的列表构建
data = [
[1, 'A'],
[2, 'B'],
[3, 'C']
]
df = pd.DataFrame(data, columns=['Column1', 'Column2'])
- 使用NumPy数组构建
import numpy as np
data = np.array([
[1, 'A'],
[2, 'B'],
[3, 'C']
])
df = pd.DataFrame(data, columns=['Column1', 'Column2'])
- 使用Series对象构建
s1 = pd.Series([1, 2, 3], name='Column1')
s2 = pd.Series(['A', 'B', 'C'], name='Column2')
df = pd.DataFrame([s1, s2])
- 使用从文件读取构建
• 从CSV文件读取
df = pd.read_csv('path_to_file.csv')
• 从Excel文件读取
df = pd.read_excel('path_to_file.xlsx')
• 从SQL数据库读取
import sqlite3
conn = sqlite3.connect('path_to_db.db')
df = pd.read_sql_query('SELECT * FROM table_name', conn)
- 使用pd.concat或pd.merge合并多个DataFrame
df1 = pd.DataFrame({'A': [1, 2, 3]})
df2 = pd.DataFrame({'B': [4, 5, 6]})
df = pd.concat([df1, df2], axis=1)
- 使用pd.DataFrame.from_records
records = [
(1, 'A'),
(2, 'B'),
(3, 'C')
]
df = pd.DataFrame.from_records(records, columns=['Column1', 'Column2'])
- 使用pd.DataFrame.from_dict构建
• 将字典的值作为列
data = {
'Column1': [1, 2, 3],
'Column2': ['A', 'B', 'C']
}
df = pd.DataFrame.from_dict(data)
• 将字典的键作为列
data = {
'Row1': [1, 'A'],
'Row2': [2, 'B'],
'Row3': [3, 'C']
}
df = pd.DataFrame.from_dict(data, orient='index')
- 使用pd.DataFrame.from_items
items = [
('Column1', [1, 2, 3]),
('Column2', ['A', 'B', 'C'])
]
df = pd.DataFrame.from_items(items)
- 使用pd.DataFrame的构造函数
df = pd.DataFrame(index=[0, 1, 2], columns=['Column1', 'Column2'])
df.loc[0] = [1, 'A']
df.loc[1] = [2, 'B']
df.loc[2] = [3, 'C']