1

I have code which is giving output in below format. How should i remove the first column and can store the elements of second row in list ? type of the output is in 'pandas.core.frame.DataFrame' format

    speed        lat       lng
1  19.130506  12.616756  7.460664   
2  63.595894  52.616838  7.460691   
3  40.740044  72.616913  7.460718   
4  22.227747  82.616993  7.460734   
5  68.058223  12.617062  7.460758  
Yuca
  • 6,010
  • 3
  • 22
  • 42
Manoj Deshpande
  • 311
  • 2
  • 18
  • first column is the index, and it's a part of the dataframe, you want to remove the index 'column'? – Yuca Sep 19 '18 at 13:16
  • If you want to remove columns, see [here](https://stackoverflow.com/questions/13411544/delete-column-from-pandas-dataframe-using-del-df-column-name). If you want to drop/reset index, see [this](https://stackoverflow.com/questions/20107570/removing-index-column-in-pandas) – Sheldore Sep 19 '18 at 13:18
  • 1
    If you want to remove speed column as the first column then yes it is possible but if you are referring first column to the index of df then you cannot drop an index because it is used by pandas to efficiently perform many operation. Yes you can convert second row to a list but that would be in-efficient. – mad_ Sep 19 '18 at 13:19
  • What output are you actually after? – Jon Clements Sep 19 '18 at 13:21
  • Ok...actually i just need to access lat and lng elements in a list. How can i only access those elements, because index is always appended in the output. – Manoj Deshpande Sep 19 '18 at 13:25
  • @mad_ : Can you please point out how can i convert elements from second row to list ? As i am planning to create geojson file with those elements – Manoj Deshpande Sep 19 '18 at 13:37
  • You can access column as `df['lat']` and `df['lng']` assuming your dataframe is `df`. Refer [pandas.DataFrame](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html). And to get list from column you can use `df['lat'].tolist()`. – nandneo Sep 19 '18 at 13:39
  • What is the expected output. There might be possibility for better answers here – mad_ Sep 19 '18 at 14:08

2 Answers2

0

Do you wanna use a values of speed like index?

if is that you can user set_index()

dataframe.set_index('speed', inplace=True)

to access the lat and lng elements you can do dataframe.loc[(speedvalue)] loc by example

dataframe.loc['19.130506']

Edit:

To transform all to a json

dataframe.set_index('speed', inplace=True).to_json(orient='index')
  • Thank you for the response. But i do not want to use 'speed' as index. I just tried with below code (df.speed) where i will get the index items and also 'speed' column items. But i only need elements of speed in list. Any inputs regarding this – Manoj Deshpande Sep 19 '18 at 13:33
  • Well... To put te elements of speed in a list you only need to do df['speed'].tolist() – Lucas Emanuel Sep 19 '18 at 13:40
  • Hi, as this one dataframe.loc['19.130506'] will be able to access a single element. I have columns of 690 elements and i need each column elements in a separate list. for ex: x = [12.616756, 52.616838,......] for lat elements – Manoj Deshpande Sep 19 '18 at 13:41
  • Use df['lat'].tolist() and df['lng'].tolist() – Lucas Emanuel Sep 19 '18 at 13:43
  • If you wanna create a json by this you can use dataframe.set_index('speed', inplace=True).to_json(orient='index') – Lucas Emanuel Sep 19 '18 at 13:46
0
  df = pd.DataFrame([
 [19.130506 ,12.616756 ,7.460664],
 [63.595894 ,52.616838 ,7.460691],
 [40.740044 ,72.616913 ,7.460718],
 [22.227747 ,82.616993 ,7.460734],
 [68.058223 ,12.617062 ,7.460758]]
  ,columns=['speed', 'lat', 'lng']);

try this :

df.iloc[1:2,1:3].values.tolist().pop()

output :

[52.616838, 7.460691]
Krishna
  • 198
  • 2
  • 10