You are first adding R0Cx and then R1Cxy. You need to add RxCy. So try:
newlist = []
row = A
col = B
for x in range (0, row):
newlist.append([])
for y in range(0, col):
newlist[x].append('R' + str(x) + 'C' + str(y))
print(newlist)
Answer from Sefe on Stack OverflowYou are first adding R0Cx and then R1Cxy. You need to add RxCy. So try:
newlist = []
row = A
col = B
for x in range (0, row):
newlist.append([])
for y in range(0, col):
newlist[x].append('R' + str(x) + 'C' + str(y))
print(newlist)
You have to fill columns in a row while still in that row:
rows = []
row = 2
col = 3
for x in range(0, row):
columns = []
for y in range(0, col):
columns.append('R' + str(x) + 'C' + str(y))
rows.append(columns)
print(rows)
will print:
[['R0C0', 'R0C1', 'R0C2'], ['R1C0', 'R1C1', 'R1C2']]
pyodbc - how to insert multiple rows from list with Python - Stack Overflow
Python: write multiple lists to multiple rows in csv - Stack Overflow
python - Assign multiple values from a list, by oder ,for multiple rows in dataframe - Stack Overflow
Look up a list of different values and return all rows in dataset that has one of these values in a given column?
You may want to add linebreak to every ending of row. You can do so by typing for example:
file.write('\n')
The csv module from standard library provides objects to read and write CSV files. In your case, you could do:
import csv
for i in range(10):
final = [i*1, i*2, i*3]
with open("0514test.csv", "a", newline="") as file:
writer = csv.writer(file)
writer.writerow(final)
Using this module is often safer in real life situations because it takes care of all the CSV machinery (adding delimiters like " or ', managing the cases in which your separator is also present in your data etc.)
I have a big data set, and I want to extract all rows that has certain values in one of the columns
Let's say I have 1000 rows, with 3 columns:
| Customer | Product | Number |
And I have a separate list of numbers (will match numbers found in column 3):
406597
504305
340405
305522
203400
Can I use python go through this separate list and return all rows from the dataset that matches either one of these numbers?
I am trying to create a chart in python using lists. I have a nested list (I think)
list = [[1, 2, 3], [4, 5, 6], [7,8,9]]
what I am trying to do is have each list in a different line so I want the output to be
1, 2, 3
4, 5, 6
7, 8, 9
so they are all in different lines and I have now created a 3x3 grid and i can access each number using its index which i think for example if I wanted to access the 5 on the middle of my chart I would do
[1][1]
row 2, position 2
(I used [1] because row 1 would be [0] and row 3 [2] (I believe))
the problem I have is that it prints it all in one single line so I am not able to create a 2d chart.
how would I create the chart using this method (or any other method sugested by you) and how would i acces each position and change its value (so for example if i wanted to access the position where the 5 currently is and turn it to a 7 so i would get
1, 2, 3
4, 7, 6
7, 8, 9
thanks.
Pandas >= 0.25
Pandas can do this in a single function call via df.explode.
df.explode('column_x')
column_a column_b column_x
0 a_1 b_1 c_1
0 a_1 b_1 c_2
1 a_2 b_2 d_1
1 a_2 b_2 d_2
Note that you can only explode a Series/DataFrame on one column.
Pandas < 0.25
Call np.repeat along the 0th axis for every column besides column_x.
df1 = pd.DataFrame(
df.drop('column_x', 1).values.repeat(df['column_x'].str.len(), axis=0),
columns=df.columns.difference(['column_x'])
)
df1['column_x'] = np.concatenate(df['column_x'].values)
df1
column_a column_b column_x
0 a_1 b_1 c_1
1 a_1 b_1 c_2
2 a_2 b_2 d_1
3 a_2 b_2 d_2
You can repeat index values:
lens = df['column_x'].str.len()
a = np.repeat(df.index.values, lens)
print (a)
[0 0 1 1]
df = df.loc[a].assign(column_x=np.concatenate(df['column_x'].values)).reset_index(drop=True)
print (df)
column_a column_b column_x
0 a_1 b_1 c_1
1 a_1 b_1 c_2
2 a_2 b_2 d_1
3 a_2 b_2 d_2
The first line should contain the first element, like this:
list_of_elements = ['AA',
'BB',
'CC',
'DD',
'EE',
'FF',
'GG']
or as Naufan Rusyda Faikar commented: Put backslash next to = Or put the left bracket next to =.
list_of_elements = \
['AA',
'BB',
'CC',
'DD',
'EE',
'FF',
'GG']
list_of_elements = [
'AA',
'BB',
'CC',
'DD',
'EE',
'FF',
'GG']
All three will work.
The best option :
list_of_elements =\
[
'AA',
'BB',
'CC',
'DD',
'EE',
'FF',
'GG'
]

