pandas df
import numpy as np
import pandas as pd
s = pd.Series([1, 3, 5, np.nan, 6, 8])
# print(s)
dates = pd.date_range('20130101', periods=6)
# print(dates)
df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
print(df)
a = list(df.itertuples(index=False))
print(a[0].A)
# print(df.iloc[0])
# for i in range(0,len(df)):
# print(df.iloc[i])
# df.apply(lambda row: print(row), axis=1)
# for index, row in df.iteritems():
# print(row) # 输出列名
# print(row[0]) # 输出列名
# print(row[1]) # 输出列名
# print(getattr(row[1],'A')) # 输出列名
"""
一,
for row in df.iteritems():
print(row) # 输出列名
('D', 2013-01-01 -0.055828
2013-01-02 0.084284
2013-01-03 0.100456
2013-01-04 -1.974011
2013-01-05 0.389353
2013-01-06 -0.481591
Freq: D, Name: D, dtype: float64)
print(row[0]) # 输出列名
D
print(row[1]) # 输出列名
2013-01-01 -0.055828
2013-01-02 0.084284
2013-01-03 0.100456
2013-01-04 -1.974011
2013-01-05 0.389353
2013-01-06 -0.481591
for index, row in df.iteritems():
print(index)
A
print(row)
2013-01-01 -1.484969
2013-01-02 -0.867084
2013-01-03 0.056310
2013-01-04 -0.455150
2013-01-05 0.407460
2013-01-06 2.217088
print(row[0])
-1.484968691334848
二,
def xxx(row):
print(row)
df.apply(lambda row: xxx(row), axis=1)
Name: 2013-01-05 00:00:00, dtype: float64
A 0.744318
B -0.408481
C -1.112620
D -0.777420
Name: 2013-01-06 00:00:00, dtype: float64
df.apply(lambda row: print(row['A']), axis=1)
1.0063752461677924
-0.13564652552723439
-0.18798539100536096
-0.5690084386213422
-0.13362146127312274
0.2806066279622273
三,
print(df.iloc[0])
A -0.472537
B 0.332931
C 0.843660
D 1.418739
for i in range(0,len(df)):
print(df.iloc[i])
A 0.746477
B 1.179014
C 1.064718
D 0.566558
Name: 2013-01-01 00:00:00, dtype: float64
.....
.....
A 1.528804
B -1.115476
C -1.977193
D 0.198156
Name: 2013-01-06 00:00:00, dtype: float64
四
a = list(df.itertuples(index=False))
print(a)
[Pandas(A=-1.1849829013487616, B=1.2936306177332026, C=0.502434532988977, D=-1.3271967698449694), Pandas(A=-0.5338415234544399, B=0.0007, D=0.7390005803766758), Pandas(A=0.4991051381749911, B=0.20302599765021992, C=0.9679266312234202, D=-0.9711976649303432), Panda752633523728, C=-0.7871250572578905, D=-0.8497460123073107)]
print(a[0])
Pandas(A=-0.5325202189360843, B=-0.6197580479253518, C=0.5830443356731136, D=-0.6202575921374842)
print(a[0].A)
1.736514718339463
"""
编程改变生活
posted on 2021-10-14 10:46 coding-farmer 阅读(112) 评论(0) 收藏 举报
浙公网安备 33010602011771号