logo
down
shadow

Getting or editing a string from a column in a csv file with pandas


Getting or editing a string from a column in a csv file with pandas

By : user2172433
Date : October 23 2020, 08:10 PM
With these it helps A row of my column named category looks like this: , Here you go:
code :
import pandas as pd

df = pd.DataFrame(
    {'col1' : [{"id":342,"name":"Web","slug":"technology/web","position":15,"parent_id":16}, 
               {"id":342,"name":"Web","slug":"technology/web","position":15,"parent_id":18}],
     'col2' : ['a', 'b']
    })

pd.concat([df, df['col1'].apply(pd.Series)['parent_id']], axis = 1).drop('col1', axis = 1)


Share : facebook icon twitter icon
Accessing and editing pandas.DataFrame via index/column entry

Accessing and editing pandas.DataFrame via index/column entry


By : StefanHa
Date : March 29 2020, 07:55 AM
may help you . i want to access a pandas DataFrame elementwise by giving the row and the column; seems simple to me, but researching didn't give the answer. I got this far: , the correct syntax is:
code :
pdf.ix[0.55,2500] = 42
Adding a column in Pandas while editing the values

Adding a column in Pandas while editing the values


By : Harry Potor
Date : March 29 2020, 07:55 AM
wish help you to fix your issue try this, assuming your HDI is in a column called 'HDI' and you are trying to create a new column that is equal to the HDI, or 0 if HDI is < .5
code :
def adj_hdi(row):
    hdi = row['HDI']
    if hdi>.5:
        return hdi
    else:
        return 0
mydataframe['ADJ_HDI'] = mydataframe.apply(lambda row: adj_hdi(row), axis = 1)
Pandas editing column based on date column

Pandas editing column based on date column


By : Nidhi
Date : March 29 2020, 07:55 AM
like below fixes the issue If you convert the column to datetime, you can use the dt accessor for vectorized operations. df['DATE'].dt.daysinmonth returns the number of days in that particular month, for example.
code :
df['DATE'] = pd.to_datetime(df['DATE'])
df['VALUE'] / df['DATE'].dt.daysinmonth
Out: 
0    3.225806
1    3.500000
2    3.290323
dtype: float64
Removing unwanted characters and editing Column names in pandas

Removing unwanted characters and editing Column names in pandas


By : jamejem
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have pandas df with certain column names. The column names are as below, , Using lambda:
code :
df.rename(columns=lambda x: x.split('/')[1].replace(' ','_') if '/' in x else x, inplace= True)

df.columns
> Index(['City', 'Regional_Level', 'District', 'Map_code', 'Age', '2015', '2016',
           '2017', '2018'],
          dtype='object')
How to loop through pandas df column, finding if string contains any string from a separate pandas df column?

How to loop through pandas df column, finding if string contains any string from a separate pandas df column?


By : user3537840
Date : March 29 2020, 07:55 AM
this will help There's no need for a loop here. Looping over a dataframe is slow and we have optimized pandas or numpy methods for almost all of our problems.
In this case, for your first problem, you are looking for Series.str.extract:
code :
dfa['country'] = dfa['sentenceCol'].str.extract(f"({'|'.join(dfb['country'])})")

           sentenceCol  other column country
0  this is from france            15  france
dfa['country'] = (
    dfa['sentenceCol'].str.extractall(f"({'|'.join(dfb['country'])})")
        .drop_duplicates()
        .to_numpy()
)

                     sentenceCol  other column country
0  this is from france and spain            15  france
1  this is from france and spain            15   spain
dfa['country'] = (
    dfa['sentenceCol'].str.extractall(f"({'|'.join(dfb['country'])})")
        .groupby(level=0)
        .agg(', '.join)
        .to_numpy()
)

                     sentenceCol  other column        country
0  this is from france and spain            15  france, spain
extraction = (
    dfa['sentenceCol'].str.extractall(f"({'|'.join(dfb['country'])})")
        .rename(columns={0: 'country'})
)

dfa = extraction.droplevel(1).join(dfa).reset_index(drop=True)

  country                    sentenceCol  other column
0  france  this is from france and spain            15
1   spain  this is from france and spain            15
dfa = pd.DataFrame({'sentenceCol':['this is from france and spain']*2,
                   'other column':[15]*2})

dfb = pd.DataFrame({'country':['france', 'spain']})
Related Posts Related Posts :
  • Submitting login form with scrapy
  • How do i edit the favicon in the Browsable API in Django REST framework?
  • multiprocessing.Pool.map_async doesn't seem to... do anything at all?
  • Python Selenium: Stale Element Reference Exception Error
  • Datetime conversion - How to extract the inferred format?
  • Import YAML variables automatically?
  • How to create a powershell shortcut for my python file
  • Python's 'set' operator doesn't work with numpy.nan
  • Pass object fields and one2many fields on same method - Odoo v8
  • Select columns based on column name and location in Pandas
  • Standardizing timeseries in Pandas using interpolation
  • How many tweets can be collected?
  • how format specifier taking value while tuple list is passed
  • How to print a numpy array with data type?
  • Timeout child thread for python3
  • How can I regroup a dataframe and accumulate a colume's values?
  • Bulk Insert into SQL Server with Python not working
  • Removing last rows of each group based on condition in a pandas dataframe
  • Why the css file can not be found in Django template?
  • targeting center of mass - scipy / numpy
  • Foursquare - get tips from VENUE_ID
  • Unpack a dictionary to format
  • encoding special characters in python2
  • Replacing integers with NaN results in the entire column becoming float dtype
  • Python 3.6 - BeautifulSoup4, parse table AttributeError: ResultSet object has no attribute 'findAll'
  • Convert panda date list to python list of date strings
  • escape response from Scrapy to parse json
  • How to create a same dropdown menu for different labels?
  • Why are some python variables uppercase whereas others are lowercase?
  • Machine Learning, What are the common techniques for feature engineering and presenting the model?
  • Modify value of a Django form field during clean() and validate again
  • Heroku Django app can't start up -- 'No module named site'
  • Getting list of dates (excluding weekends)
  • Im trying to create the regular expression to include the text and not the href
  • Python file.readline(2) reads first 2 charectars
  • Groupby with handling empty bin in python
  • Modifying Gcode
  • calling a value in a dictionary within a dictionary (reading a json file)
  • Bouncing ball invalid syntax why is that?
  • Python making a counter
  • Python rstrip and split
  • What does the String mean in numpy.r_?
  • How to correctly extend variable __all__ in a __init__.py?
  • Python behaves weird with piped input
  • Python 3 two dimensional list comprehension
  • How to slice image by broadcasting slices? Error: 'only integer scalar arrays can be converted to a scalar index' in pyt
  • (Python Beginner) Need a start on classes
  • IndexError: At least one sheet must be visible
  • How to solve a system of linear equations over the nonnegative integers?
  • Pandas keep the most complete rows
  • "List index out of range" error in Python Memory Match game
  • Numpy: how to use argmax results to get the actual max?
  • Google Cloud Dataflow can't import 'google.cloud.datastore'
  • Calculate pandas DataFrame column by custom routine which accepts dictionary as input
  • Connect to a Class Method by it's method name holded into a var in a for loop in python
  • PyQt5 signals and threading.Timer
  • Replace 2 characters in a string in python
  • Passing command line arguments from a folder script to a file script
  • Understand the syntaxe X[Y == c] in Numpy
  • Optimize beginner python script about substring replacement
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org