- 方法1
import pandas as pd
df = pd.DataFrame(columns = ['A','B','C','D'])
for i in range(4):
s = pd.Series(['A1','B4', 'C4', 'D4'], index=data.columns, name = i)
df = df.append(s, ignore_index=True)
print(df)
A B C D
0 A1 B4 C4 D4
1 A1 B4 C4 D4
2 A1 B4 C4 D4
3 A1 B4 C4 D4
- 方法2 :按照索引添加行
import pandas as pd
df = pd.DataFrame(columns = ['A','B','C','D'])
index = ['a','b','c','d']
for i in range(4):
df.loc[index[i]] = ['A'+str(i),'B'+str(i),'C'+str(i),'D'+str(i)]
print(df)
A B C D
a A0 B0 C0 D0
b A1 B1 C1 D1
c A2 B2 C2 D2
d A3 B3 C3 D3