I want to ask questions about Panda's Excel!

Asked 2 years ago, Updated 2 years ago, 72 views

There are worksheets called A and B There is no separate seat setting for both.

A has written all the data that could come out of B in the first column The second column contains the values that you want the output of the first column that is the same row.

(The first column of A contains an unconditional integer, but it is not known whether the type is a string or an integer, and the second column contains an unconditional string.)

In the first column of B, the data in the first column of A is randomly placed in each row There is nothing written in the second column. (Similar to B, it is not known whether the type of number in the first column is a string or an integer, and even if you point to data such as A, you may not have the same data type.)

A.xlsx example

Day 1
Two
Three, three
Four companies
Five and five

B.xlsx example

3
1
1
2
4
1

In this case, call B

When using the above example data, After all the work, B wants to be in the following state.

33
1st
1st
Two
Four companies
1st

I understand how to do it, but I can't think of a specific way to implement it. I don't know if I'm asking the right question because it's my first time posting it, but is there a proper way to use Pandas?

python pandas excel openpyxl xlsx

2022-09-20 15:43

1 Answers

>>> import pandas as pd

>> a = pd.DataFrame({"A":[1, 2, 3, 4, 5], "B":list("two, three, four, five")})
>>> b = pd.DataFrame({"C":[ 2, 3, 3, 1, 1, 4 ]})
>>> print(a.to_markdown())
|    |    |   A | B   |
|---:|----:|:----|
|  0 | 1 | Day |
|  1 | 2 | This |
Two, three, three.
|  three | four | four | four |
|  4 | 5 | O |
>>> print(b.to_markdown())
|    |    |   C |
|---:|----:|
|  0 |   2 |
|  1 |   3 |
|  2 |   3 |
|  3 |   1 |
|  4 |   1 |
|  5 |   4 |
>>> b.merge(a, left_on="C", right_on="A")
   C  A  B
Two, two, two
One, three, three, three
Two, three, three, three
Three, eleven, eleven days
Four, eleven, eleven days
Five, four, four, four
>>> c = b.merge(a, left_on="C", right_on="A")
>>> c
   C  A  B
Two, two, two
One, three, three, three
Two, three, three, three
Three, eleven, eleven days
Four, eleven, eleven days
Five, four, four, four
>>> c.drop(columns="A")
   C  B
02 is
One, three, three
Two, three, three
31 days
41 days
Five, four, four

>>> a.to_records(index=False)
(['), (3, ' this ', ' (1), (2, ' three ' and ' Oh ', ' buy '), (4), (5)], array rec.
          dtype=[('A', '<i8'), ('B', 'O')])
>>> mapping = { e[0]:e[1] for e in a.to_records(index=False) }
>>> mapping
{'work' : 1, 2 : 3 : 'three', 4 : 5 : 'Oh', 'buy', 'this'}.
>>> b
   C
0  2
1  3
2  3
3  1
4  1
5  4
>>> b["D"] = b["C"].map(mapping)
>>> print(b.to_markdown())
|    |   C | D   |
|---:|----:|:----|
This zero.
1 3 three.
Two, three, three.
3 days 1
A 1 4.
|  5 | 4 | Sa |


2022-09-20 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.