以下是一些可用转化的数据类型和输出结果:
1. 字典(Dictionary)
data = {'Name': ['Tom', 'Nick', 'John', 'Alice'],'Age': [20, 21, 19, 18],'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)
输出:
Name Age City
0 Tom 20 New York
1 Nick 21 Los Angeles
2 John 19 Chicago
3 Alice 18 Houston
2. 列表(List)
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
df = pd.DataFrame(data)
输出:
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9
3. NumPy数组(NumPy Array)
import numpy as np
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df = pd.DataFrame(data)
输出:
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9
4. 列表的列表(List of Lists)
data = [[1, 'Alice'], [2, 'Bob'], [3, 'Charlie']]
df = pd.DataFrame(data, columns=['ID', 'Name'])
输出:
ID Name
0 1 Alice
1 2 Bob
2 3 Charlie
5. 其他pandas对象
s1 = pd.Series([1, 2, 3], name='A')
s2 = pd.Series(['Alice', 'Bob', 'Charlie'], name='B')
df = pd.DataFrame([s1, s2])
输出:
A B
0 1 Alice
1 2 Bob
2 3 Charlie
6. 标量值(Scalar)
data = 5
df = pd.DataFrame([data])
输出:
0
5
7. 嵌套字典(Nested Dictionary)
data = {'Person': {'Name': ['Tom', 'Nick', 'John', 'Alice'],'Age': [20, 21, 19, 18]},'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)
输出:
Person CityName Age Name
0 Tom 20 New York
1 Nick 21 Los Angeles
2 John 19 Chicago
3 Alice 18 Houston
请注意,这些输出是基于pandas的标准设置,实际输出可能会根据你的pandas版本和环境设置有所不同。