迭代 Pandas 数据框的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49862853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over rows of Pandas dataframe
提问by Rocky
I have df like:
我有 df 喜欢:
CELLID lon lat METER LATITUDE_SM LONGITUDE_SM Path_ID
2557709 5.286339 51.353820 E0047000004028217 51.3501 5.3125 2557709_E0047000004028217
For each Path_ID(str) I would like to iterate the loop and would like to achieve df1 like:
对于每个 Path_ID(str) 我想迭代循环并想实现 df1 像:
Path_ID METER LATITUDE_SM LONGITUDE_SM
2557709_E0047000004028217 E0047000004028217 51.3501 5.3125
Path_ID CELLID Lon lat
2557709_E0047000004028217 2557709 5.286339 51.353820
I have many rows in the df. I am doing something like
我在 df 中有很多行。我正在做类似的事情
for row in df.iterrows():
print row ['Path_ID'],row['METER'],row['LATITUDE_SM'], row ['LONGITUDE_SM']
回答by jpp
It is unclear why you want this behaviour, but you can achieve this with pd.DataFrame.iloc
.
目前尚不清楚您为什么想要这种行为,但您可以使用pd.DataFrame.iloc
.
If you only need specific columns, replace :
with a list of column numbers.
如果您只需要特定的列,请替换:
为列号列表。
import pandas as pd, numpy as np
df = pd.DataFrame(np.random.random((5, 5)))
for i in range(len(df.index)):
print(df.iloc[[i], :])
# 0 1 2 3 4
# 0 0.587349 0.947435 0.974285 0.498303 0.135898
# 0 1 2 3 4
# 1 0.292748 0.880276 0.522478 0.081902 0.187494
# 0 1 2 3 4
# 2 0.692022 0.908397 0.200202 0.099722 0.348589
# 0 1 2 3 4
# 3 0.041564 0.980425 0.899634 0.725757 0.569983
# 0 1 2 3 4
# 4 0.787038 0.000077 0.213646 0.444095 0.022923
回答by rafaelc
It is very hard to understand your goal, but IIUC, you want to group by Path_ID
and print each value
很难理解你的目标,但是 IIUC,你想分组Path_ID
并打印每个值
grouped_df= df.groupby("Path_ID")[["Path_ID", "METER", "LATITUDE_SM", "LONGITUDE_SM"]]
for key, val in grouped_df:
print grouped_df.get_group(key), "\n"
Output
输出
Path_ID METER LATITUDE_SM LONGITUDE_SM
0 2557709_E0047000004028217 E0047000004028217 51.35 5.3125