i'd like to ask a simple question. I want to save the last row of a dataframe to an excel sheet last rows without colname of dataframe.
Dataframe:
date Name Age
2019/10/1 Kate 18
2019/10/2 Jim 20
2019/10/3 James 23
excel sheet:
date Name Age
2019/9/29 Rose 18
2019/9/30 Eva 20
I want to add dataframe values to excel last row,something like this
excel sheet new:
date Name Age
2019/9/29 Rose 18
2019/9/30 Eva 20
2019/10/1 Kate 18
2019/10/2 Jim 20
2019/10/3 James 23
code:
app = xw.App(visible=False, add_book=False)
wb=app.books.open(path_xl)
sht=wb.sheets[sht_name]
rng=sht.range("A1")
last_rows=rng.current_region.rows.count
sht.range('A'+str(last_rows+1)).value=df.values
but the result saved in excel sheet is wrong, i dont know why
Related
I have to separate .txt file into small pieces, based on the matched value. For example, I have .txt file looks like:
Names Age Country
Mark 19 USA
John 19 UK
Elon 20 CAN
Dominic 21 USA
Andreas 21 UK
I have to extract all rows with the same value “Age” and to copy them to other file or perfom some other action..
How it is possible to be done with Python, I have never do that before.
Thank you in advance :)
I am asking, because of I have no idea how it should be done. The excpected result is to have this data separated:
Names Age Country
Mark 19 USA
John 19 UK
Names Age Country
Elon 20 CAN
Names Age Country
Dominic 21 USA
Andreas 21 UK
Here is a possible solution:
with open('yourfile.txt') as infile:
header = next(infile)
ages = {}
for line in infile:
name, age, country = line.rsplit(' ', 2)
if age not in ages:
ages[age] = []
ages[age].append([name, age, country])
for age in ages:
with open(f'age-{age}.txt', 'w') as agefile:
agefile.writeline(header)
agefile.writelines(ages[age])
For the sample you posted, the code above will leave you with files named age-19.txt, age-20.txt, and age-21.txt, with the contents separated by age, as you requested.
If you have them all in a list you can use something like this...
alltext = ["Names Age Country", "Mark 21 USA", "John 21 UK","Elon 20 CAN","Dominic 21 USA", "Andreas 21 UK"]
Canada = [alltext[0]] #Creates a list with your column header
NotCanada = [alltext[0]] #Creates a list with your column header
for row in alltext[1:]:
x = row.split()
if x[2] == "CAN":
Canada.append(row)
else:
NotCanada.append(row)
print(Canada)
print(NotCanada)
Will print two different lists of your separated players.
['Names Age Country', 'Elon 20 CAN']
['Names Age Country', 'Mark 21 USA', 'John 21 UK', 'Dominic 21 USA', 'Andreas 21 UK']
Mathew Jim 60
Gerry Hagger 61
Sam Page 23
Azli Muzan 52
David Agor 32
James Paine 40
Mike Gregor 63
Howard Jack 56
I have this data in CSV file with names and age in one column and would like a python script that will separate the names from the age, create a new CSV with the heights of the individuals classified as short, medium and tall.
Height Classification
<5 short
5-6 medium
6> tall
Will like two column headings of Names and Classification.
I need to compare two columns together: "EMAIL" and "LOCATION".
I'm using Email because it's more accurate than name for this issue.
My objective is to find total number of locations each person worked
at, sum up the total of locations to select which sheet the data
will been written to and copy the original data over to the new
sheet(tab).
I need the original data copied over with all the duplicate
locations, which is where this problem stumps me.
Full Excel Sheet
Had to use images because it flagged post as spam
The Excel sheet (SAMPLE) I'm reading in as a data frame:
Excel Sample Spreadsheet
Example:
TOMAPPLES#EXAMPLE.COM worked at WENDYS,FRANKS HUT, and WALMART - That
sums up to 3 different locations, which I would add to a new sheet
called SHEET: 3 Different Locations
SJONES22#GMAIL.COM worked at LONDONS TENT and YOUTUBE - That's 2 different locations, which I would add to a new sheet called SHEET:
2 Different Locations
MONTYJ#EXAMPLE.COM worked only at WALMART - This user would be added
to SHEET: 1 Location
Outcome:
data copied to new sheets
Sheet 2
Sheet 2: different locations
Sheet 3
Sheet 3: different locations
Sheet 4
Sheet 4: different locations
Thanks for taking your time looking at my problem =)
Hi Check below lines if work for you..
import pandas as pd
df = pd.read_excel('sample.xlsx')
df1 = df.groupby(['Name','Location','Job']).count().reset_index()
# this is long line
df2 = df.groupby(['Name','Location','Job','Email']).agg({'Location':'count','Email':'count'}).rename(columns={'Location':'Location Count','Email':'Email Count'}).reset_index()
print(df1)
print('\n\n')
print(df2)
below is the output change columns to check more variations
df1
Name Location Job Email
0 Monty Jakarta Manager 1
1 Monty Mumbai Manager 1
2 Sahara Jonesh Paris Cook 2
3 Tom App Jakarta Buser 1
4 Tom App Paris Buser 2
df2 all columns
Name Location ... Location Count Email Count
0 Monty Jakarta ... 1 1
1 Monty Mumbai ... 1 1
2 Sahara Jonesh Paris ... 2 2
3 Tom App Jakarta ... 1 1
4 Tom App Paris ... 2 2
I have 2 separate excel spreadsheets
spreadsheet 1 is as such:
ID tin name date
1 21043 Bob 8/1/2019
2 45667 Jim 7/1/2018
3 69780 Sal 4/24/2017
The 2nd spreadsheet is as such:
ID tin job
1 21043 02
2 76544 02
3 45667 04
I am trying to figure out how to match the 2 spreadsheets and make 1 list as such:
ID tin name date job
1 21043 Bob 8/1/2019 02
2 45667 Jim 7/1/2018 04
3 69780 Sal 4/24/2017
4 76544 02
the common denominator is the "tin" but i have to merge the ones that duplicate, but then add the ones from both sheets that dont duplicate..
I am new to python and VERY new to xlrd so i cannot seem to even figure out the best terms to use to google an example.
I found some information on a next(iter statement but after countless attempts i could not figure out a useful way to use it to combine.
Is there an easy way or am i "up a creek"??
Thank you,
Bob
You can use pandas for this. Pandas uses xlrd and other excel readers under the hood.
You will do something like this:
df1 = pandas.read_excel('file1.xls', sheet_name='...')
df2 = pandas.read_excel('file2.xls', sheet_name='...')
df1.merge(df2, how='outer')
You may need some variation of this depending on your column names.. see pandas merge
So I had a dataframe and I had to do some cleansing to minimize the duplicates. In order to do that I created a dataframe that had instead of 40 only 8 of the original columns. Now I have two columns I need for further analysis from the original dataframe but they would mess with the desired outcome if I used them in my previous analysis. Anyone have any idea on how to "extract" these columns based on the new "clean" dataframe I have?
You can merge the new "clean" dataframe with the other two variables by using the indexes. Let me use a pratical example. Suppose the "initial" dataframe, called "df", is:
df
name year reports location
0 Jason 2012 4 Cochice
1 Molly 2012 24 Pima
2 Tina 2013 31 Santa Cruz
3 Jake 2014 2 Maricopa
4 Amy 2014 3 Yuma
while the "clean" dataframe is:
d1
year location
0 2012 Cochice
2 2013 Santa Cruz
3 2014 Maricopa
The remaing columns are saved in dataframe "d2" ( d2 = df[['name','reports']] ):
d2
name reports
0 Jason 4
1 Molly 24
2 Tina 31
3 Jake 2
4 Amy 3
By using the inner join on the indexes d1.merge(d2, how = 'inner' left_index= True, right_index = True) you get the following result:
name year reports location
0 Jason 2012 4 Cochice
2 Tina 2013 31 Santa Cruz
3 Jake 2014 2 Maricopa
You can make a new dataframe with the specified columns;
import pandas
#If your columns are named a,b,c,d etc
df1 = df[['a','b']]
#This will extract columns 0, to 2 based on their index
#[remember that pandas indexes columns from zero!
df2 = df.iloc[:,0:2]
If you could, provide a sample piece of data, that'd make it easier for us to help you.