Reputation: 141
I have df like:
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 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
for row in df.iterrows():
print row ['Path_ID'],row['METER'],row['LATITUDE_SM'], row ['LONGITUDE_SM']
Upvotes: 1
Views: 6387
Reputation: 59284
It is very hard to understand your goal, but IIUC, you want to group by Path_ID
and print each value
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
Upvotes: 2
Reputation: 164823
It is unclear why you want this behaviour, but you can achieve this with 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
Upvotes: 2