logo
down
shadow

Adding a column to a pandas dataframe based on cell values


Adding a column to a pandas dataframe based on cell values

By : Edmund Cheng
Date : October 24 2020, 08:10 AM
wish of those help Use pd.merge:
code :
df1.merge(df2, on = ['user','col1'])

   user col1 col2 col3
0    1    a   a1   a2
1    1    b   b1   b2
2    1    c    c   c2
3    2    a   a2   a3


Share : facebook icon twitter icon
adding a column to Pandas dataframe based on adjacent values of existing column

adding a column to Pandas dataframe based on adjacent values of existing column


By : Inam Ullah
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Assuming you first sort by timestamp, you could group by id, and for each group, shift the values of red once and twice, and find the logical or of the result:
code :
 df['col'] = df.red.groupby(df.id).apply(lambda g: g | g.shift(-1) | g.shift(-2))
In [100]: df = pd.DataFrame({'red': [True, True, True, False, False, True, True, True], 'id': [0] * 6 + [1] * 2})

In [101]: df.red.groupby(df.id).apply(lambda g: g | g.shift(-1) | g.shift(-2))
Out[101]: 
0    True
1    True
2    True
3    True
4    True
5    True
6    True
7    True
Name: red, dtype: bool
Adding a column to pandas dataframe based on bool values in existing column

Adding a column to pandas dataframe based on bool values in existing column


By : Drew Dastardly
Date : March 29 2020, 07:55 AM
I hope this helps . You can use numpy.where:
code :
import numpy as np
data_2015['color_code'] = np.where(data_2015['isfixed'], 'Blue', 'Green')
df = pd.DataFrame({'isfixed': [True, False, True]})

df
Out: 
  isfixed
0    True
1   False
2    True


df['color_code'] = np.where(df['isfixed'], 'Blue', 'Green')

df
Out: 
  isfixed color_code
0    True       Blue
1   False      Green
2    True       Blue
Adding values to pandas dataframe with function based on other column in dataframe

Adding values to pandas dataframe with function based on other column in dataframe


By : Pulkit
Date : March 29 2020, 07:55 AM
should help you out I agree with you that using the 'type' column as a label-based index will make things easier. With this done, you can iterate over the rows of the first dataframe, then add owned value to the appropriate row in the second dataframe using the .loc method.
code :
for row_1 in df_1.itterrows():
  owned_value = row_1[1]['owned'] #iterrows() enumeration generator over rows      
  mechanics =  row_1[1]['mechanics_split']
  for type_string in mechanics:
    df_2.loc[type_string,('owned')] += owned_value
Adding values to new Pandas dataframe column based on partial string contents of existing column

Adding values to new Pandas dataframe column based on partial string contents of existing column


By : l3ch3ro
Date : March 29 2020, 07:55 AM
Does that help I have data stored as a dataframe using Python Pandas. Among the columns, I have a "Product" column which contains the brand name and model (e.g. Nike Air Jordan, Adidas Gazelle). I want to create a new column that just contains the brand (e.g. Nike, Adidas), which I will later use in groupby to summarize the data. From my research, I believe contains and regex can be used to do this. However, the implementation has not worked. I've also seen different approaches, some using "for i in range" while others do it as a replace in a single line of code. , IIUC:
code :
shoes_df['brand'] = shoes_df.Product.str.extract(pat='(Nike|Adidas|Asics)',expand=False)
            Product  Unit sales   brand
0     Nike vaporfly        1500    Nike
1      Nike Jordans        1600    Nike
2  Adidas supernova        2341  Adidas
3      Asics Kayano        1345   Asics
4      Asics GT2010        4523   Asics
5    Adidas gazelle        2345  Adidas
6      Nike air max        1634    Nike
7       Nike Lebron        3129    Nike
Adding columns to a pandas dataframe based on values of another column

Adding columns to a pandas dataframe based on values of another column


By : Piotr Nowakowski
Date : March 29 2020, 07:55 AM
Hope that helps The values of eis, ref and her switch to "25" because you are looping over the variable PrimaryServiceCategory, and the last value in that serie is "25". You are using eis, ref and her as the names of the iterator variable, so they change in every loop. I think this is an inefficient way to do it. It's better if you use groupby and transform:
code :
df['count'] = df.groupby(['id_profile','PrimaryServiceCategory']).transform('count')
Related Posts Related Posts :
  • Receiving unsupported operand error while comparing random number and user input.
  • How to wrap the process of creating start_urls in scrapy?
  • How to mark 'duplicated sequence' in pandas?
  • Boolean indexing on multidimensionnal array
  • Unmodified column name index in patsy
  • Cleaner way to unpack nested dictionaries
  • Importing a python module to enable a script to be run from command line
  • Maya Python read and set optionMenu value via variable
  • How can I bind a property to another property in Kivy?
  • Python extracting specific line in text file
  • How to implement n-body simulation with pymunk?
  • Python / matplotlib: print to resolution and without white space / borders / margins
  • Sum up the second value from one dictionary with all values from another dictionary
  • Robot Framework: Open a chrome browser without launching URL
  • Generate inline Bokeh scatterplots in Jupyter using a for loop
  • Group list of dictionaries python
  • Efficient way to apply multiple Boolean mask to set values in a column using pandas
  • Lazy evaluation of a Python dictionary
  • id of xpath is getting changed every time in selenium python 2.7 chrome
  • Matplotlib RuntimeWarning displaying a 3D plot
  • Cannot install pyqt5 for python3.4 on windows 10
  • Gravity Problems
  • Where to position `import` modules inside an class?
  • Python OpenCV: Cannot resize image
  • Print on the same spot in IPython console
  • Disable logging except in tests
  • Writing json to file in s3 bucket
  • Sorting numpy array created by laspy
  • Open an XML file through URL and save it
  • How to build a 2-level dictionary?
  • error installing scipy using pip on windows 10
  • __str__ from my own matrix, python
  • python re how to Extract fields use findall()?
  • how to read a value from text HI file using python?
  • How to use horizontal scrolling in treeview,here i use tree view to make a table
  • Dependant widgets in tkinter
  • Read and write in a JSON file using python 2.x
  • How to fix the function issue while allowing it to be dynamic?
  • Set long strings as default value in class
  • What is the REGEX for any number with a string(letters and punctuations)?
  • pip with several version of python on windows
  • 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?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org