1. dataframe --> dict
import pandas as pd
df = pd.read_csv(file_path, sep=sep) # sep: 间隔字符
df.to_dict(orient='dict') # 默认dict
# {'time': {0: "-3'", 1: "-6'", 2: "-9'", 3: "-12'", 4: "-15'", 5: "-18'"}, 'lon': {0: 113.05818604, 1: 113.08431951, 2: 113.09787861, 3: 113.10432351, 4: 113.10988356, 5: 113.11937187}, ......}
df.to_dcit(orient='list')
# {'time': ["-3'", "-6'", "-9'", "-12'", "-15'", "-18'"], 'lon': [113.05818604, 113.08431951, 113.09787861, 113.10432351, 113.10988356, 113.11937187], ......}
df.to_dict(orient='records')
# [{'time': "-3'", 'lon': 113.05818604, 'lat': 34.94490635, 'alti': 1002.53, 'line': 1}, {'time': "-6'", 'lon': 113.08431951, 'lat': 34.95138556, 'alti': 941.59, 'line': 1}, {'time': "-9'", 'lon': 113.09787861, 'lat': 34.95491861, 'alti': 1103.84, 'line': 1}, {'time': "-12'", 'lon': 113.10432351, 'lat': 34.95662099, 'alti': 1436.42, 'line': 1}, {'time': "-15'", 'lon': 113.10988356, 'lat': 34.95836725, 'alti': 61.09, 'line': 1}, {'time': "-18'", 'lon': 113.11937187, 'lat': 34.9616488, 'alti': 868.69, 'line': 1} ......]
df.to_dict(orient='split')
# {'index': [0,1,2,3,4,5....], 'columns': ['time', 'lon', 'lat', 'alti', 'line'], 'data': [["-3'", 113.05818604, 34.94490635, 1002.53, 1], ["-6'", 113.08431951, 34.95138556, 941.59, 1], ["-9'", 113.09787861, 34.95491861, 1103.84, 1], ["-12'", 113.10432351, 34.95662099, 1436.42, 1], ["-15'", 113.10988356, 34.95836725, 61.09, 1]......]}
df.to_dict(orient='index') # 键为数字类型
# [0:{'time': "-3'", 'lon': 113.05818604, 'lat': 34.94490635, 'alti': 1002.53, 'line': 1}, 1:{'time': "-6'", 'lon': 113.08431951, 'lat': 34.95138556, 'alti': 941.59, 'line': 1}, 2:{'time': "-9'", 'lon': 113.09787861, 'lat': 34.95491861, 'alti': 1103.84, 'line': 1}, 3:{'time': "-12'", 'lon': 113.10432351, 'lat': 34.95662099, 'alti': 1436.42, 'line': 1}, 4:{'time': "-15'", 'lon': 113.10988356, 'lat': 34.95836725, 'alti': 61.09, 'line': 1}, 5:{'time': "-18'", 'lon': 113.11937187, 'lat': 34.9616488, 'alti': 868.69, 'line': 1} ......]
2. 循环 dataframe
for row in df.itertuples(index=True) # 默认包含 index
print(row) # Pandas(Index=0, time="-3'", lon=113.05818604, lat=34.94490635, alti=1002.53, line=1)
print(getattr(row, 'time')) # -3'
标签:输出,alti,python,lon,df,time,lat,line,pandas
From: https://www.cnblogs.com/jessecheng/p/17172910.html