I'm struggling with a for loop for a dataframe.
I want a function where I loop through a dataframe with object names and their properties.
Suggest the dataframe looks like this:
data = [['object 1', 'property 1'], ['object 1','property 11'], ['object 2', 'property 2'],['object 2','property 22'], ['object 3', 'property 3'],['object 3','property 33']]
I want to generate a string where the last row of each object doesn't contain a comma and all other rows don't.
def addProperties(objects):
obj = objects
for index, row in obj.iterrows():
if row['label'] =! #last element
string = row['label'] + row['attribuutLabel'] + ','
else:
string = row['label'] + row['attribuutLabel']
return string
Output should be something like this:
string = 'object 1 property 1, property 11, property 111 object 2 property 2, property 22 property 3, property 33'
I'm quite new to python so don't know what the best way is to achieve this.
Can someone help out?
Related
I was trying to generate a dictionary which is with a value of list which contains certain values from google sheet cell. I was able to get the output separately each time but not able to append those values together into a list.
Here is the print format of the cell values(get_SW_data) I'm working on,
[['SW1', 'Port 1', 'CSW1'], ['SW1', 'Port 2', 'CSW2'], ['SW2', 'Port 1', 'CSW1'], ['SW2', 'Port 1', 'CSW1']]
I'm trying to get SW1 port values into a list and add it into a dictionary.
Here is the code,
for x in get_SW_data:
if x[0] == "SW1":
port = x[1]
port_dict = {
"ports" : port
}
print(port_dict)
This gives me the output,
{'ports': 'Port 1'}
{'ports': 'Port 2'}
But I want something like this,
{'ports': ["Port 1", "Port 2"]}
You can create empty list first and append port value to the list in each iteration. Create a dictionary data using the list.
ports_list = []
for x in get_sw_data:
if x[0] == 'SW1':
ports_list.append(x[1])
port_dict = {'ports': ports_list}
print(port_dict)
Within the Dash code I have
dcc.Store(id='store-data', data=[], storage_type='memory')
Where I created and stored some variables.
Later, I used a callback to calculate some results based on the stored data.
The stored data returns a dictionary and looks something like this:
dict_values = {
'Value A':[1,2,3],
'Value B': [4,5,6],
'Value C' :[7,8,9],
}
Now, within the callback that has as input the stored data I tried to create a DataFrame like this:
df = pd.DataFrame(dict_values['Value A'])
That's when I get the error TypeError: list indices must be integers or slices, not str
Does anyone know why and how can I fix it?
I have to mentioned that even thou I've got this error, Dash app still runs and works without any problem.
rename your dictionary
import pandas as pd
d = {
'Value A':[1,2,3],
'Value B': [4,5,6],
'Value C' :[7,8,9],
}
pd.DataFrame(d['Value A'])
0
0 1
1 2
2 3
have a df with type (str)
name password
mark (('name', 'value passed'),)
cuban (('location', 'area geocode'),)
(('name', 'value passed'),) is a string
convert a dataframe column passwordof type str to list of type tuple
expected output :
tuples_password = [(('name', 'value passed'),),(('location', 'area geocode'),)]
tried this :
tuples = [tuple(x) for x in df.password]
Im a total noob and though Id give it a go.
df['tuple_field'] = [tuple(eval(i)) for i in df['password']]
tuples = [x for x in df['tuple_field']]
Output:
[(('name', 'value passed'),), (('location', 'area geocode'),)]
The standard code for filtering through pandas would be something like:
output = df['Column'].str.contains('string')
strings = ['string 1', 'string 2', 'string 3']
Instead of 'string' though, I want to filter such that it goes through a collection of strings in list, "strings". So I tried something such as
output = df['Column'].str.contains('*strings')
This is the closest solution I could find, but did not work
How to filter pandas DataFrame with a list of strings
Edit: I should note that I'm aware of the | or operator. However, I'm wondering how to tackle all cases in the instance list strings is changing and I'm looping through varying lists of changing lengths as the end goal.
You can create a regex string and search using this string.
Like this:
df['Column'].str.contains('|'.join(strings),regex=True)
you probably should look into using isin() function (pandas.Series.isin) .
check the code below:
df = pd.DataFrame({'Column':['string 1', 'string 1', 'string 2', 'string 2', 'string 3', 'string 4', 'string 5']})
strings = ['string 1', 'string 2', 'string 3']
output = df.Column.isin(strings)
df[output]
output:
Column
0 string 1
1 string 1
2 string 2
3 string 2
4 string 3
Hello Python community,
I have a problem with my code creation.
I wrote a code that creates dynamically dataframes in a for loop. The problem is that I don't know how to access to them.
Here is a part of code
list = ['Group 1', 'Group 2', 'Group 3']
for i in list:
exec('df{} = pd.DataFrame()'.format(i))
for i in list:
print(df+i)
The dataframes are created but i can not access them.
Could someone help me please?
Thank you in advance
I'm not sure exactly how your data is stored/accessed but you could create a dictionary to pair your list items with each dataframe as follows:
list_ = ['Group 1', 'Group 2', 'Group 3']
dataframe_dict = {}
for i in list_:
data = np.random.rand(3,3) #create data for dataframe here
dataframe_dict[i] = pd.DataFrame(data, columns=["your_column_one", "two","etc"])
Can then retrieve each dataframe by calling its associated group name as the key of the dictionary as follows:
for key in dataframe_dict.keys():
print(key)
print(dataframe_dict[key])