python
复制代码
import pandas as pd
# 创建一个DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charles', 'David', 'Edward'],
'Age': [25, 27, 22, 32, 29],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
}
df = pd.DataFrame(data)
print(df)
#指定索引
df2 = pd.DataFrame(data = {
'Name': ['Alice', 'Bob', 'Charles', 'David', 'Edward'],
'Age': [25, 27, 22, 32, 29],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']},
index=[1,2,3,4,5])
print(df2)
#指定列的顺序
df3 = pd.DataFrame(data = {
'Name': ['Alice', 'Bob', 'Charles', 'David', 'Edward'],
'Age': [25, 27, 22, 32, 29],
'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']},
index=[1,2,3,4,5],
columns=['City','Age','Name'])
print(df3)