Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

python - python3:如何打印groupby.last()?(python3: how to print groupby.last()?)

$ cat n2.txt
apn,date
3704-156,11/04/2019
3704-156,11/22/2019
5515-004,10/23/2019
3732-231,10/07/2019
3732-231,11/15/2019

$ python3
Python 3.7.5 (default, Oct 25 2019, 10:52:18) 
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd 
>>> df = pd.read_csv("n2.txt")
>>> df
        apn        date
0  3704-156  11/04/2019
1  3704-156  11/22/2019
2  5515-004  10/23/2019
3  3732-231  10/07/2019
4  3732-231  11/15/2019
>>> g = df.groupby('apn')
>>> g.last()
                date
apn                 
3704-156  11/22/2019
3732-231  11/15/2019
5515-004  10/23/2019
>>> f = g.last()

>>> for r in f.itertuples(index=True, name='Pandas'):
...     print(getattr(r,'apn'), getattr(r,'date'))
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'Pandas' object has no attribute 'apn'

>>> for r in f.itertuples(index=True, name='Pandas'):
...     print(getattr(r,"apn"), getattr(r,"date"))
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
AttributeError: 'Pandas' object has no attribute 'apn'

What is the proper way to print this to a file?

(将其打印到文件的正确方法是什么?)

eg.

(例如。)

apn, date
3704-156,11/22/2019
3732-231,11/15/2019
5515-004,10/23/2019
  ask by AG1 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code should be changed:

(您的代码应更改:)

df = pd.read_csv("n2.txt")
g = df.groupby('apn')
f = g.last()

Use Series.to_csv because output of f is pandas Series :

(使用Series.to_csv因为f输出是pandas Series :)

f.to_csv(file)

Or use DataFrame.to_csv with convert index to 2 columns DataFrame :

(或者将DataFrame.to_csv与将index转换为2列DataFrame :)

f.reset_index().to_csv(file, index=False)

Or use solution with DataFrame.drop_duplicates :

(或将解决方案与DataFrame.drop_duplicates一起DataFrame.drop_duplicates :)

df = pd.read_csv("n2.txt")
df = df.drop_duplicates('apn', keep='last')
df.to_csv(file, index=False)

In your solution use Index for select index of Series :

(在您的解决方案中,将Index用于Series选择index :)

for r in f.itertuples(index=True, name='Pandas'):
    print(getattr(r,'Index'), getattr(r,'date'))
3704-156 11/22/2019
3732-231 11/15/2019
5515-004 10/23/2019

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...