python pandas read dataframe and do not include index [duplicate] - python

This question already has answers here:
Pandas dataframe hide index functionality?
(8 answers)
Closed 3 years ago.
very simple question
I am reading an excel sheet with python and I want to print the results without the automatic index pandas adds
import pandas as pd
x=pd.read_excel(r'2_56_01.276295.xlsx',index_col=None)
print x[:3]
this prints the 1st 3 rows
blahblah Street Borough
0 55 W 192 ST Bronx
1 2514 EAST TREMONT AV Bronx
2 877 INTERVALE AV Bronx
but I do not want the index

print x.to_string(index=False)
should do the trick

Related

How to replace row value with only the integers within the value using Pandas? [duplicate]

This question already has answers here:
How can I remove all non-numeric characters from all the values in a particular column in pandas dataframe?
(6 answers)
Closed 2 years ago.
I have a bunch of columns like this
District
________
State District 1
State District 2
State District 3
4th State House District
5th State House District
State District 6
...
State District 17
I want to transform each of these so it only contains the integer value:
District
________
1
2
3
4
5
6
...
17
How can this be done with Pandas? Is this even possible with Pandas or would I have to perform this transformation with SQL or some database language?
A simple solution using str.replace would be to just strip off all non numeric characters:
df['District'] = df['District'].str.replace('\D', '')

Splitting .txt file in python [duplicate]

This question already has answers here:
Customizing the separator in pandas read_csv
(4 answers)
Closed 2 years ago.
I have a .txt file that is separated as follows for multiple rows:
Vermont;VT;Tunbridge;95000204;Republican;John Kasich;36;0.319
When read with pandas I only get 1 column.
How do I split the data in python so that each separated value is a different column in a pandas dataframe
Thanks
Like this (see https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html for more)
import pandas as pd
df = pf.read_csv('data.csv',sep=';')
print(df)
where data.csv is
Vermont;VT;Tunbridge;95000204;Republican;John Kasich;36;0.319
NewYork;VT;Tunbridge;95000204;Republican;John Kasich;36;0.88
output
Vermont VT Tunbridge ... John Kasich 36 0.319
0 NewYork VT Tunbridge ... John Kasich 36 0.88
[1 rows x 8 columns]

Pandas - remove duplicate items completely from dataframe [duplicate]

This question already has answers here:
Drop all duplicate rows across multiple columns in Python Pandas
(8 answers)
Closed 2 years ago.
I want to remove duplicate items completely from a pandas dataframe. For example, I have the dataframe:
location area
0 mountain view 1044ft2
1 palo alto None
2 mountain view 890ft2
3 san carlos 1000ft2
4 belmont None
What I want to do is find unique values in column location and remove any items that had duplicates altogether, completely, etc.. So the final product will look like this (notice mountain view is gone):
location area
1 palo alto None
3 san carlos 1000ft2
4 belmont None
Thanks.
Use
df.drop_duplicates(subset='location', keep=False)

Using pandas dataframe style for adjusting column width [duplicate]

This question already has answers here:
Is there a way to auto-adjust Excel column widths with pandas.ExcelWriter?
(16 answers)
Closed 2 years ago.
I have a dataframe df:
A B LongColName1 AnotherNa AnotherName3
Brunner Island is not island Baltimore is town 0.26 3.88 3.75
Brunner Island is not island Baltimore is town -0.59 1.47 2.01
When I dump the above dataframe to excel, it appears as following in excel:
Is there a way to style the dataframe so that dump to excel looks as following:
One approach could be to find the max length of column and set the width of that column explicitly while writing to excel.
Consider below dataframe:
In [527]: df
Out[527]:
A
0 Brunner Island is not island
1 Brunner Island is not an island
len_max = df.A.str.len().max()
from StyleFrame import StyleFrame
excel_writer = StyleFrame.ExcelWriter(filename)
sf = StyleFrame(df)
sf.set_column_width(columns=['A'],width=len_max)
sf.to_excel(excel_writer=excel_writer)
excel_writer.save()
There is no way to auto adjust column width. But there are some workarounds mentioned in this post Is there a way to auto-adjust Excel column widths with pandas.ExcelWriter?

Data Manipulation in Python [duplicate]

This question already has answers here:
Pandas groupby with delimiter join
(2 answers)
pandas groupby concatenate strings in multiple columns
(1 answer)
Closed 4 years ago.
I am dealing with a data set which has the following fields:
ID Person_Name Person_Country
110 Marc CA
110 Sean CN
111 Matt IN
111 Rob AU
112 Mike US
I intend grouping the data in the following way:
ID Person_Name Person_Country
110 Marc; Sean CA; CN
111 Matt; Rob IN; AU
112 Mike US
I tried using the built-in functions like .pivot_table() and .unstack(), but they weren't helpful since I am dealing with non-numeric data.

Categories