logo
down
shadow

Access atribute of every object in pandas dataframe column


Access atribute of every object in pandas dataframe column

Content Index :

Access atribute of every object in pandas dataframe column
Tag : python , By : evegter
Date : November 28 2020, 11:01 PM

will be helpful for those in need You need first convert to_datetime and then use dt.month:
print (pd.to_datetime(df.Date).dt.month)
0    10
1    10
2     9
3     9
Name: Date, dtype: int64
print (df.Date.apply(lambda x: x.month))
0    10
1    10
2     9
3     9
Name: Date, dtype: int64
#[40000 rows x 2 columns]
df = pd.concat([df]*10000).reset_index(drop=True)

In [292]: %timeit (df.Date.apply(lambda x: x.month))
100 loops, best of 3: 15.8 ms per loop

In [293]: %timeit (pd.to_datetime(df.Date).dt.month)
100 loops, best of 3: 5.44 ms per loop

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Pandas: access data from dataframe by row and column number


Tag : python , By : Raghaw
Date : March 29 2020, 07:55 AM
this will help I have a simple program made me confused. I read a 3 * 10 data from a csv file, and I want to access a particular data by its row and column number. But it failed, I doun't know why. , Indexing starts from 0:
In [8]:

df
Out[8]:
    0   1   2   3   4   5   6   7   8   9
0   1   2   3   4   5   6   7   8   9  10
1  11  12  13  14  15  16  17  18  19  20
2  21  22  23  24  25  26  27  28  29  30

In [11]:

df[2][2]
Out[11]:
23
In [13]:

df[3][2], df[5][2]
Out[13]:
(24, 26)
df[3][3]

How I can speed up row column access to pandas dataframe?


Tag : python , By : Meg
Date : March 29 2020, 07:55 AM
I hope this helps you . You can use iat:
print product_list.category_name.iat[int(prod)-1]
print product_list.brand_name.iat[int(prod)-1]
product_list = pd.DataFrame({'brand_name': {'r': 'r', 'g': 't', 'w': 'i'}, 
                             'category_name': {'r': 's', 'g': 'f', 'w': 'a'}})
print product_list
  brand_name category_name
g          t             f
r          r             s
w          i             a

In [242]: %timeit product_list.iloc[int(prod)-1]['category_name']
The slowest run took 8.27 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 82.7 µs per loop

In [243]: %timeit product_list.brand_name.iat[int(prod)-1]
The slowest run took 16.01 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 9.96 µs per loop
product_list = pd.DataFrame({'brand_name': {0: 't', 1: 'r', 2: 'i'}, 
                             'category_name': {0: 'f', 1: 's', 2: 'a'}})
print product_list
  brand_name category_name
0          t             f
1          r             s
2          i             a

In [250]: %timeit product_list.iloc[int(prod)-1]['category_name']
The slowest run took 8.24 times longer than the fastest. This could mean that an intermediate result is being cached 
10000 loops, best of 3: 84.7 µs per loop

In [251]: %timeit product_list.brand_name.iat[int(prod)-1]
The slowest run took 24.17 times longer than the fastest. This could mean that an intermediate result is being cached 
100000 loops, best of 3: 9.86 µs per loop

Proper way to access a column of a pandas dataframe


Tag : python , By : Lucas Thompson
Date : March 29 2020, 07:55 AM
like below fixes the issue Using . as a column accessor is a convenience. There are many limitations beyond having spaces in the name. For example, if your column is named the same as an existing dataframe attribute or method, you won't be able to use it with a .. A non-exhaustive list is mean, sum, index, values, to_dict, etc. You also cannot reference columns with numeric headers via the . accessor.
So, yes, ['col'] is strictly better than .col because it is more consistent and reliable.

Python Pandas NLTK Tokenize Column in Pandas Dataframe: expected string or bytes-like object


Tag : python , By : user134570
Date : March 29 2020, 07:55 AM
it helps some times There is probably a non-string-like object (such as NaN) in your actual df['TEXT'] which is not shown in the data you posted.
Here is how you might be able to find the problematic values:
mask = [isinstance(item, (str, bytes)) for item in df['TEXT']]
print(df.loc[~mask])
df = df.loc[mask]
df['TEXT'] = df['TEXT'].astype(str)
import pandas as pd
from nltk.tokenize import sent_tokenize, word_tokenize 

df = pd.DataFrame({'ID': [1, 2, 3, 4],
                   'TEXT': ['cat, dog fish',
                            'turtle; cat; fish fish',
                            'hello book fish',
                            np.nan]})
#    ID                    TEXT
# 0   1           cat, dog fish
# 1   2  turtle; cat; fish fish
# 2   3         hello book fish
# 3   4                     NaN

# df['TEXT'].apply(word_tokenize)
# TypeError: expected string or buffer


mask = [isinstance(item, (str, bytes)) for item in df['TEXT']]
df = df.loc[mask]
#    ID                    TEXT
# 0   1           cat, dog fish
# 1   2  turtle; cat; fish fish
# 2   3         hello book fish
In [108]: df['TEXT'].apply(word_tokenize)
Out[108]: 
0                [cat, ,, dog, fish]
1    [turtle, ;, cat, ;, fish, fish]
2                [hello, book, fish]
Name: TEXT, dtype: object

Access 1st column in Pandas dataframe


Tag : python-3.x , By : dlouzan
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • python, sort list with two arguments in compare function
  • Is it possible to print from Python using non-ANSI colors?
  • Pandas concat historical data using date minus some number of days
  • CV2: Import Error in Python OpenCV
  • Is it possible to do this loop in a one-liner?
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • Create a plot for each unique ID
  • a thread python with 'while' got another thread never start
  • Solution from SciPy solve_ivp contains oscillations for a system of first-order ODEs
  • trigger python events driven by selenium controlled browser
  • Passing line-edits to a contextmanager to set validators
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com