Python - Flag row without using loop - python

I would like to know if there's any function that I can use for flag (when there's pop up date. for ex:row 188,189,190...etc.) without using loop. please see attached excel file.
Thank you so much!
an example

If you are ok with using pandas...
import pandas as pd
path = 'path\to\file'
df = pd.read_csv(path)
flags = ~df['pop up'].isna()

Related

Using CSV file in Django choices

I have this function that calls the choices and activates them in each Model, however, I would like to know if there is a way to use this CSV file automatically, without having to call it item by item...
def Equipe():
with open("controle_pav/static/texto/equipes.csv", 'r') as arquivo:
equipes = arquivo.read()
EQUIPE = (
('equipes', equipes.split()[0]),
('equipes', equipes.split()[1]),
('equipes', equipes.split()[2]),
)
return EQUIPE
Is there a way to use an iterator or something like that. ?
I tried to make a router with the for but I was not successful
You can use pandas to read the CSV as follows:
import pandas as pd
equipes = pd.read_csv("controle_pav/static/texto/equipes.csv", sep=" ", header=None)
equipes will be a pandas DataFrame, which supports all kinds of manipulation.
Pandas library could be your solution:
https://pandas.pydata.org/
Please explain more about your csv file and what your Equipe function should return exactly if you need further help

GUI for editing and saving a python pandas dataframe

In a python function I want to show the user a pandas dataframe and let the user edit the cells in the dataframe. My function should use the edited values in that dataframe (i.e. they should be saved).
I've tried pandasgui, but it does not seem to return the edits to the function.
Is there a function/library I can use for this?
Recently solved this problem with dtale
import pandas as pd
import dtale
df = pd.read_csv('table_data.csv')
dt = dtale.show(df) # create dtale with our df
dt.open_browser() # bring it to a new tab (optional)
df = dt.data # connect all the updates from dtale gui and our df
# (so rn if u edit any cell, you will immediately get the result saved in ur df)
Yesterday I came across with some bugs while using dtale. Filtering broke my changes and creating some new rows I dont need.
Usually I use dtale and pandasgui together.
Hope it helps!

Python - Pandas module column rename not working correctly

I am starting to learn and understand panda module in Python. However, my issue is with the rename string. The rename works fine when i use print, this shows the column has been renamed:
print(data.rename(columns={"Rep": "Name"}))
However, when i use print(data), to show all of the data from the document, the column does not show as being renamed. This also does not show when the file has been exported using the data.to_csv("example.csv") string.
Would really appreciate if somebody could shed some light on this please.
Full Source code below:
import pandas as pd
data = pd.read_excel(r"D:\Downloads\Book1.xlsx")
del data["Region"]
del data["Item"]
print(data.rename(columns={"Rep": "Name"})
print(data)
data.to_csv("example.csv")
Use inplace argument, to make the changes reflect in the DataFrame as well, like this:
data.rename(columns={"Rep": "Name"}, inplace = True)
Try adding 'inplace=True' to data.rename
print(data.rename(columns={"Rep": "Name"}, inplace=True))

Pandas: how to edit values in a column of a .csv file?

I have a .csv file which looks as follows:link
I want to open this file using pandas and edit the column Coordinate by adding a constant value of 756 to each value in it. Finally, I want the changes to be reflected in the .csv file.
How do I do that?
Edit: What I had been doing is as follows (#EdChum):
df = pd.read_csv('C:/TestBook1.csv')
df = df[['Coordinate','Speed']]
Coord = df['Coordinate']
Coord = Coord + 756
This is where I was going wrong. From here it would have been a messy affair to save changes into the .csv file.
you can also type:
df["Coordinate"] = df["Coordinate"] + 756
#EdChum: Thanks for your comment. It kind of fired me up. I was unnecessarily complicating things for myself. Following is what I did:
df = pd.read_csv('C:/TestBook1.csv')
df = df[['Coordinate','Speed']]
df['Coordinate']+=756
df.to_csv('C:/TestBook1.csv')
Initially I was loading all the values of the column into a variable and trying to find a way to save it. After your comment I thought of experimenting and I am glad that it worked for me.
Define path where csv file is located
Location = r'C:\\'
df = pd.read_csv(Location,header=None)
df["Coorinate"].values +756
Do not forget to import pandas package
import pandas as pd

searching a csv file and then outputting a term associating to the term

I am creating a code, that read a csvs file and search for a specific item code and then it will output the name of the item.
How would i do this?
I don't have any code yet
Thanks
You can use pandas
Install it, then try
import pandas as pd
df = pd.read_csv('yourFile.csv')
print df

Categories