How do I remove name and dtype from pandas output
Use the.values
attribute.Example:
s = pd.Series(['race','gender'],index=[1,2])
print(s)
Out[159]:
1 race
2 gender
dtype: object
s.values
array(['race', 'gender'], dtype=object)
You can convert to a list or access each value:
list(s)
['race', 'gender']
python pandas: removing name and dtype in iterrows
You should use the Series.to_string
method before printing:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,10,(3,4)))
for name,s in df.iterrows():
print(f'row: {name}')
print(s.to_string()) # to_string outputs only the data, not the metadata
example:
row: 0
0 9
1 0
2 9
3 5
row: 1
0 1
1 2
2 5
3 6
row: 2
0 0
1 1
2 5
3 5
Remove name, dtype from pandas output of dataframe or series
You want just the .values
attribute:
In [159]:
s = pd.Series(['race','gender'],index=[311,317])
s
Out[159]:
311 race
317 gender
dtype: object
In [162]:
s.values
Out[162]:
array(['race', 'gender'], dtype=object)
You can convert to a list or access each value:
In [163]:
list(s)
Out[163]:
['race', 'gender']
In [164]:
for val in s:
print(val)
race
gender
remove dtype info from pandas mode values
Do:
import pandas as pd
# dummy setup
mode_list = ['Race', 'Education', 'Gender', 'Married', 'Housing', 'HH size']
df = pd.DataFrame(data=[['White', 'High School', 'Female', 'Single', 'Rental', 1]], columns=mode_list)
# extract mode
mode_values = df[mode_list].mode().values[0].tolist()
print(mode_values)
Output
['White', 'High School', 'Female', 'Single', 'Rental', 1]
Delete `dtype: object` in pandas DataFrame print
'Address'
is the name of the pd.Series
and 'Name'
is the name of the index
Try:
s.rename_axis(None).rename(None)
Coco Bubble Tea : 129 E 45th St New York, NY 10017
Gong Cha : 75 W 38th St, New York, NY 10018
Smoocha Tea & Juice Bar : 315 5th Ave New York, NY 10016
dtype: object
Or I'd rewrite your function
def boba_recs(lat, lng):
f1 = pd.read_csv("./boba_final.csv")
user_loc = Point(lng, lat) # converts user lat/long to point object
# makes dataframe of distances between each boba place and the user loc
f1['Distance'] = [user_loc.distance(Point(xy)) for xy in zip(f1.Longitude, f1.Lat)]
# grabs the three smallest distances
boba = f1.nsmallest(3, 'Distance').set_index('Name') # sets index to name
return(": " + boba['Address']).rename_axis(None).rename(None)
If you want a string
def boba_recs(lat, lng):
f1 = pd.read_csv("./boba_final.csv")
user_loc = Point(lng, lat) # converts user lat/long to point object
# makes dataframe of distances between each boba place and the user loc
f1['Distance'] = [user_loc.distance(Point(xy)) for xy in zip(f1.Longitude, f1.Lat)]
# grabs the three smallest distances
boba = f1.nsmallest(3, 'Distance').set_index('Name') # sets index to name
temp = (": " + boba['Address']).rename_axis(None).__repr__()
return temp.rsplit('\n', 1)[0]
Related Topics
How to Make a Roll the Dice Command With My Discord Bot
Spark Equivalent of If Then Else
How to Continue a Loop After Catching Exception in Try ... Except
Pandas: Subtracting Two Date Columns and the Result Being an Integer
Parentheses and Quotation Marks in Output
How to Save All the Variables in the Current Python Session
Remove Partial String from Dataframe With Pandas
How to Call a Django Function on Button Click
How to Increase Dpi With Opencv
How to Find Rows of One Dataframe in Another Dataframe
How to Concatenate Multiple Column Values into a Single Column in Pandas Dataframe
Manual Function to Sort 3 Numbers, Lowest to Highest
Python Pandas- Find the First Instance of a Value Exceeding a Threshold
Python + Beautifulsoup: How to Get 'Href' Attribute of 'A' Element
How to Read a CSV File from an S3 Bucket Using Pandas in Python