1

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']
3
  • If you already have the df why are you using a loop to print it out? Does print(df) not suffice? Commented Apr 16, 2018 at 17:17
  • I want to have latitude longitude of meter for path_ID , and for same path_id latitude longitude of CELLID. so basically split it into two raws for a complete data frame Commented Apr 16, 2018 at 17:19
  • If you truly want to print row by row, see jpp's solution. But you can likely save yourself a lot of time by just printing a subset of the dataframe, where you slice the columns you want. print(df[['Path_ID', 'CEILID', 'Lon', 'lat']]) for instance Commented Apr 16, 2018 at 17:23

2 Answers 2

2

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 
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.